Linux Network Overview

Source: Internet
Author: User
Linux Network overview-general Linux technology-Linux technology and application information. For more information, see the following section. I started to pay attention to my blog and thought it was not good enough. But to get started, upload it first. This article only contains dry texts, but some of the descriptions are not very clear. I hope you can get some new understanding from it.

I. linux Network Hierarchy
The linux network layer is as follows:
BSD socket (the corresponding data structure is socket-> proto_ops, and sock_iocb is also included in the socket. The data structure is msghdr)
Inet Socket is the transport layer TCP and UDP (corresponding data structure is sock data structure, sock_common is included in it; inet_sock-> proto structure contains sock data structure. Data packets are expressed in the sk_buff structure)
IP layer ()
NIC Driver

Ii. global variables of linux network
1. Global variable of the notification structure, static struct notifier_block * netdev_chain;
When ioctl is called for a nic, every socket should be reflected.
2. static struct net_proto_family * net_families [NPROTO];
The top-layer protocol structures such as af_unix, af_inet, and af_packet are placed in this file.
This part is equivalent to the domain concept mentioned in BSD. It is used to support different network architecture, such as iso and tcpip.
3. All the variables of the proto type should be linked to static LIST_HEAD (proto_list);, of course, these variables will eventually be embedded into the sock struct.
4. packet_type Data Structure
The data structure is used to build a data transmission bridge between the protocol stack and network devices.
There are two global variables:
Static struct list_head ptype_base [16];
Static struct list_head ptype_all;
Call the dev_add_pack () function to add the packet_type variable to the hash table,
When receiving data, the netif_receive_skb () function traverses the ptype_base linked list, calls each rcv function, and passes in the skb structure.
In the net/ipv4/af_inet.c file, the variable ip_packet_type is statically defined to indicate the IP package structure.
In the net/ipv4/arp. c file, the variable arp_packet_type is statically defined to indicate the arp packet structure.
5. Common global variables for network device Interfaces
In the Linux kernel, the struct net_device represents a network device interface. The kernel function register_netdev is used to register a network device interface with the kernel. The following describes the data structures related to network device registration.
Dev_base is a global pointer to a struct net_device that points to a linked list containing all network device interfaces in the current system. Every time you call register_netdev to register a new network device interface, add a new item at the end of the linked list.
Dev_name_head is a list array, which is actually a hash table. All network devices in the system put their names into this hash table as hash keys, to provide interfaces such as dev_get_by_name, so that you can find the device by the device name.
Dev_index_head is also a hash table in the form of a list array. All network devices in the system will be assigned an index number during registration. It is a value increasing in the order of device registration. All network devices use their assigned index numbers as hash key values and put them in this hash table to provide interfaces such as dev_get_by_index, so that devices can be found through device index numbers.
Netdev_chain is a global linked list of struct notifier_block. When a network device interface is registered or its status changes, you need to call the member function in each item of the linked list to notify relevant modules, the device has changed. If a module needs to be notified of changes to the network device, it only needs to define a struct notifier_block and provide a notification callback function. Therefore, to register a network device, call notifier_call_chain to notify all relevant modules that a new network device interface has been registered.
6. global variables corresponding to softnet_data
Each CPU corresponds to one, which is a struct of the struct softnet_data type,
Struct softnet_data
{
Struct net_device * output_queue;
Struct sk_buff_head input_pkt_queue;
Struct list_head poll_list;
Struct sk_buff * completion_queue;
Struct net_device backlog_dev;
};
The member input_pkt_queue is a core-level receiving data queue. The qlen Member of the struct sk_buff_head records the length of the receiving queue. In the kernel, the length of the receiving queue is limited to mynetdev_max_backlog, and its value is defined as 1000.

Iii. Data Structure and global variables for the inet Protocol
1. The structure proto_ops is the operation function structure defined in the Socket structure of the BSD socket layer. All the implementation functions of this structure work at the INET Socket layer. Actually, they all point to specific functions at the INET Socket layer. For example, tcp and udp direct to inet_stream_ops and inet_dgram_ops respectively.
2. The corresponding data structure is sock data structure, where sock_common is included. The inet protocol implements a data structure, inet_sock, which uses the data structure sock as a member. The structure proto is the control function of the INET Socket layer. It defines different protocol families. tcp and udp correspond to tcp_prot and udp_prot respectively.
The struct sock data structure is stored in the File include/net/sock. h.
Struct proto is in the file include/net/sock. h.
498/* Networking protocol blocks we attach to sockets.
499 * socket layer-> transport layer interface
500 * transport-> network interface is defined by struct inet_protosw
501 */
3. Registration of all transport layer protocols
The role of the net_protocol type is to provide a receiving function for this Protocol. After the network layer receives the datagram, it determines that it is a datagram of a protocol based on the protocol field in the IP header, then find the receiving function in the global variable inet_protos.
In the File include/net/protocol. h defines the structure in the file net/ipv4/protocol. c defines the following global variables. All transport layer protocols TCP, UDP, ICMP, and IGMP must be registered in the following array.
Struct net_protocol * inet_protos [MAX_INET_PROTOS];
# Define MAX_INET_PROTOS 256
Use the inet_add_protocol function to add the protocol number to the hash table inet_protos,
Use inet_del_protocol to delete a protocol from the hash table inet_protos.
The Protocol variables tcp_protocol, udp_protocol, igmp_protocol, and icmp_protocol are statically defined in the net/ipv4/af_inet.c file.
4. Data Structure struct inet_protosw.
The so-called structure struct inet_protosw is a service type provided by some transport layer protocols. For example, SOCK_STREAM corresponds to reliable data stream services, SOCK_DGRAM corresponds to unreliable data packet services, and SOCK_RAW corresponds to the original IP layer services.
In the inet_protosw function, there are both struct proto pointers and struct proto_ops pointers.
Corresponds to the static global variable static struct inet_protosw inetsw_array [] = {}
The static global variable defines three socket types: SOCK_STREAM, SOCK_DGRAM, and SOCK_RAW.
For ipv4, SOCK_STREAM corresponds to tcp, SOCK_DGRAM corresponds to udp, and SOCK_RAW corresponds to raw ip.
In this case, the same socket type only corresponds to one item in the Protocol Stack Array inetsw_array. Different protocol types of the same socket are connected by a linked list.
5. the correspondence between the inet_protosw structure array inetsw_array and the net_protocol structure array inet_protos is: The inet_protosw: protocol field is an integer, which is the index of the inet_protos [] array of the net_protocol Structure Type.
Use inet_protos [inet_protosw: protocol] to obtain the inet_protosw type variable.
Related Article

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.