The Network namespace (net_namespace) in the kernel represented by the struct net struct body. In the Linux kernel, each network device (struct Net_device) has a network namespace that belongs to it, and as a network device is the network namespace that belongs to the kernel, the network device (struct Net_device) is introduced. In the Linux kernel, use a struct net_device to portray a network device.
In the Linux kernel, the network namespace struct net struct contains multiple fields, and in this article only some of these fields are used to describe the macro architecture of the network namespace in the kernel.
struct NET
{
Used to organize all the network namespaces in the kernel in a doubly-linked list, which will
All struct NET structures are organized into doubly linked lists;
struct list_head list;
//Because there may be multiple network devices in a network namespace, and these network devices are also
Organized in the form of doubly linked lists. And Dev_base_head is a double-linked list of network equipment.
The linked list head;
struct List_head dev_base_head;
//Because in a network namespace, each network device has its device name, and the kernel
Can quickly find the appropriate network equipment according to the network device name, using the common in the kernel
Hash lists are implemented to quickly find devices based on the device name.
struct Hlist_head *name_hlist;
In a network device namespace, each network device will have a unique in the system
interface index value (int ifindex), and the kernel is also hashed through the kernel hash list
To quickly find network devices based on interface index values.
struct Hlist_head *index_hlist;
}
through the introduction of some fields in the network namespace in the kernel, it is understood that the network namespace (struct net) in the kernel is organized in the form of a doubly linked list, while the doubly linked list in the Linux kernel has a head node. Where is the head node of the network namespace double-linked list in the kernel?
The following structure is defined in the Linux kernel and exported, allowing the user to traverse, find, and add network namespaces.
List_head (net_namespace_list);
EXPORT_SYMBOL_GPL (net_namespace_list);
In the Linux kernel, by default, there will be a default network namespace named Init_net
, and also export it as a global variable.
struct NET init_net;
EXPORT_SYMBOL_GPL (init_net);
Add a network namespace to the kernel:
struct net *net_create (void);//This function is used to add a network to the kernel
namespaces;
This function mainly does three things:
1. A struct net struct body is assigned through the struct net *net_alloc (void) function
2. By setup_net (struct net *ns) function, the allocated struct NET structure body is set up accordingly;
3. Finally, add the allocated struct net struct body to the Net_namespace_list's double-linked list tail.
Release a network namespace:
void Net_free (struct net *ns);
Traverse all the network namespaces in the kernel:
# define For_each_net (Var) \
List_for_each_entry (Var, &net_namespace_list, List)
# define FOR_EACH_NET_RCU (Var) \
List_for_each_entry_rcu (Var, &net_namespace_list, List)
Summary:
The network namespace, struct NET, is through one of the following:
The struct List_head Dev_base_list organizes all of the network devices in the form of a doubly-linked list.
struct Hlist_head *name_hlist is used to implement a network device name to quickly find network equipment (struct net_device);
The struct Hlist_head *index_hlist is used to implement a network device's interface index value to quickly find network devices.
struct net *net_create (void) is used to add a network namespace to the kernel
void Net_free (struct net *ns) to release a network namespace in the kernel
For_each_net (struct net *var) for traversing the network namespace in the kernel
FOR_EACH_NET_RCU (struct net *var)
This article is from the "FAI Aberdeen" blog, please make sure to keep this source http://weiguozhihui.blog.51cto.com/3060615/1584610
struct NET Network namespace