System and method for exchanging messages between kernel mode and user mode
By using the exchange mechanism of read and write function modules between the kernel state and the user state, the problem of invalid query when exchanging messages between the kernel state and the user state is solved, and efficient message transmission and stable system operation are achieved.
Patent Information
- Application Number
- CN202111268626.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-10-29
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2041-10-29
AI Technical Summary
In the existing technology, when exchanging messages between the kernel state and the user state, the user state program needs to continuously query the shared memory, resulting in a large number of invalid operations and occupying CPU resources when there is no data to be sent in the kernel state.
By calling the system read function module in the kernel mode and the system write function module in the user mode, and using the read and write function pointers in the file_operations structure, message exchange between the kernel mode and the user mode is realized. The user mode process does not send packets in the kernel. Time blocking to avoid invalid queries.
It reduces CPU usage, improves the efficiency of message transmission between kernel mode and user mode, and enhances system reliability and stability.
Smart Images

Figure CN114003408B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to a method and system for exchanging messages between kernel mode and user mode. More specifically, the present disclosure relates to a method and system for exchanging messages between kernel mode and user mode by calling a system read function module in kernel mode and a system write function module in user mode. Background Art
[0002] Due to technological advancement and the development of the market economy, in order to further improve the competitiveness of products to adapt to market and customer needs and quickly develop new equipment and put it on the market, network equipment suppliers not only need to improve the performance of equipment, but also need to continuously improve the portability and stability of products.
[0003] Specifically at the code level, whether a program runs in kernel mode or user mode has a great impact on its portability and stability.
[0004] When a process running in kernel state occupies the CPU, it can access all data in memory and control hardware devices. This means that kernel state programs have the highest running rights. However, the higher the running rights, the greater the impact of defects in the program, and the more unstable the system. Moreover, since kernel state programs can control hardware, the hardware of different devices is not the same. Therefore, when developing new devices, the code of the original device needs to be significantly modified before it can be used on the new device, which will undoubtedly prolong the development cycle of the new device and fail to meet the needs of the market and users in a timely manner.
[0005] Processes running in user mode have lower execution rights than kernel mode and cannot access all memory or manipulate hardware devices. They need to go through system calls to enter kernel mode. However, this also means that user mode programs are more portable and have a lower chance of fatal defects than kernel mode programs. Although user mode programs are slightly less efficient than kernel mode programs, these differences are negligible given the current level of development of hardware devices. Because programs are decoupled from hardware, user mode programs have better portability and stability.
[0006] Therefore, consider running the most core and commonly used functional modules in network devices in kernel mode and running other modules in user mode. In order for modules to collaborate to complete related tasks, a large amount of message data needs to be transmitted between kernel mode and user mode.
[0007] In order to exchange a large amount of message data between kernel state and user state, the prior art solution creates a memory shared by user state and kernel state, in which a value is stored. The user state program determines whether there is data to be sent in kernel state by reading this value, and if there is data to be sent, the data is received. Therefore, under the prior art, the user state program still needs to start a thread to continuously read the value in the shared memory. Although reading memory consumes less energy than system calls, when there is no data to be sent in kernel state, there are still a large number of invalid operations in the system, which occupy CPU resources.
[0008] Therefore, there is a need for a method and system for transmitting message data between kernel mode and user mode, in which when no message is sent in kernel mode, a user mode program does not need to perform a large number of invalid query operations. Summary of the invention
[0009] In order to solve the above-mentioned problems in the prior art, the present disclosure proposes a method and system for exchanging messages between kernel state and user state by calling a system read function module in kernel state and calling a system write function module in user state.
[0010] According to one aspect of the present disclosure, a system for exchanging messages between kernel mode and user mode is provided, the system comprising: an association creation component, used to create a file operation structure to associate a system call component and a device driver component, so that each member name in the file operation structure corresponds to a call; an interface creation component, used to create an exchange interface between the kernel and the user, so as to execute the user's operation of accessing the system kernel data, so that the kernel space and the user space can exchange information; a message exchange component, used to start the exchange interface based on user instructions, read the corresponding function pointer in the file operation structure by executing the system call of the file operation structure, so as to then transfer the control to the function, thereby completing the device driver.
[0011] According to the system for exchanging messages between kernel mode and user mode disclosed in the present invention, the file operation structure is a file_operations structure, and the pointer declarations of the read and write function modules in the structure are respectively: ssize_t (*read) (struct file *, char __user *, size_t, loff_t *), ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *), wherein the pointer parameter __user is a buffer for placing information.
[0012] According to the system for exchanging messages between kernel mode and user mode disclosed in the present invention, the exchange interface is a proc file named channel_net_dev file, and its structure is: proc_create("channel_net_dev",00777, NULL,&g_netdev_fops);
[0013] According to the system for exchanging messages between kernel state and user state disclosed in the present invention, after the channel_net_dev file is opened in the user state, a read function module is called according to the channel_net_dev file to transfer data from kernel space to user space, and a write function module is called in the user state to transfer data from user space to kernel space.
[0014] According to the system for exchanging messages between kernel state and user state disclosed in the present invention, the write function module calls the copy_from_user function module in kernel state to copy the user state data in the buffer indicated by the pointer parameter __user to the kernel state according to the file_operations structure.
[0015] According to the system for exchanging messages between kernel mode and user mode disclosed in the present invention, the channel_net_dev file specifies the read and write functions drv_netdev_read and drv_netdev_write as follows:
[0016] const struct file_operations g_netdev_fops =
[0017] {
[0018] .owner = THIS_MODULE,
[0019] .read = drv_netdev_read,
[0020] .write = drv_netdev_write,
[0021] .open = drv_netdev_open,
[0022] .release = drv_netdev_release,
[0023] .unlocked_ioctl = drv_netdev_ioctl,
[0024] };
[0025] Therefore, after opening the channel_net_dev file in the user state, the read function is called to call the kernel state drv_netdev_read to transfer data from the kernel space to the user space, and the write function module is called to call the kernel state drv_netdev_write function module to transfer data from the user space to the kernel space.
[0026] According to the system for exchanging messages between kernel state and user state disclosed in the present invention, the user_netdev_write function module is assigned to the packet sending function port_start_xmit of the first virtual port, so that the packet sending function of the first virtual port is called to send the message from the user state to the kernel state.
[0027] According to the system for exchanging messages between kernel state and user state disclosed in the present invention, it also includes a kernel structure creation component, which is used to create a kernel state struct drv_netdev_info_s structure of the following structure:
[0028] struct drv_netdev_info_s
[0029] {
[0030] struct sk_buff_head skb_list; / *kernel packet list* /
[0031] wait_queue_head_t tx_wait; / *Packet waiting queue* /
[0032] };
[0033] This kernel state structure is used to save the kernel packet list skb_list and the packet waiting queue tx_wait.
[0034] After the system is started and initialized, a user_netdev_hard_xmit_thread process is created in user mode. The process is used to monitor whether the kernel has sent packets through the kernel packet list and packet waiting queue information saved in the kernel state struct drv_netdev_info_s structure.
[0035] According to the system for exchanging messages between kernel state and user state disclosed in the present invention, the user_netdev_hard_xmit_thread process opens the channel_net_dev file in a blocking manner, and repeatedly calls the read function module in an infinite loop, and then calls the drv_netdev_read function module in the kernel state.
[0036] According to the system for exchanging messages between kernel state and user state disclosed in the present invention, each time the drv_netdev_read function module in the kernel state is called, the current process is added to the waiting queue tx_wait.
[0037] According to the system for exchanging messages between kernel state and user state disclosed in the present invention, before calling the read function module, the process takes out a piece of memory space from a memory pool in user state, and passes the first address pkb of the memory space to drv_netdev_read through the second parameter __user of the read function.
[0038] According to the system for exchanging messages between kernel mode and user mode disclosed in the present invention, when the kernel does not send a packet, the process is blocked from executing downward in the drv_netdev_read function module to avoid invalid query operations during idle time.
[0039] According to the system for exchanging messages between kernel state and user state disclosed in the present invention, when the kernel is ready to send a packet, the socket buffer (skb) to be sent is added to the kernel packet sending list skb_list, and the blocked process in the waiting queue tx_wait is awakened to allow the drv_netdev_read function to continue to execute, an skb is taken out from the kernel packet sending list skb_list, and reassembled into the format of the user state message, and the newly assembled user state message is copied to the memory space pointed to by pkb through the copy_to_user function module to complete this data transmission.
[0040] According to the system for exchanging messages between kernel state and user state disclosed in the present invention, the drv_netdev_hard_xmit function module is assigned to the packet sending function hard_start_xmit of the second virtual port, and the function prototype of hard_start_xmit is as follows:
[0041] int (*hard_start_xmit) (struct sk_buff * skb, struct net_device *dev);
[0042] Among them, sk_buff is a structure used to send data packets, so as to call the drv_netdev_hard_xmit function module by calling the packet sending function of the second virtual port.
[0043] According to another aspect of the present disclosure, a method for transmitting messages from user state to kernel state in the aforementioned system is also provided, comprising the following steps: when data is ready to be sent from user state to kernel state, calling the user_netdev_write function module in user state; and after the user_netdev_write function opens the channel_net_dev file, calling the drv_netdev_write in kernel state through the write function, and copying the user state data to kernel state through the passed-in __user pointer and copy_from_user function to complete data sending.
[0044] The method for sending a message comprises the following steps: after the system is started and initialized, a process user_netdev_hard_xmit_thread is created in the user state, the process opens the channel_net_dev file created by the channel_net_dev creation module in the kernel state in a blocking manner, and repeatedly calls the read function in an infinite loop, and then calls the drv_netdev_read function in the kernel state, each time the drv_netdev_read function is entered, the current process is added to the waiting queue tx_wait, so as to monitor whether the kernel has sent a packet according to the kernel packet sending linked list skb_list, wherein before calling the read function module, the program takes out a piece of memory space from a memory pool in the user state, and reads the first address pkb of the memory space through the read The second parameter __user of the function module is passed to drv_netdev_read; when the kernel does not send a packet, it blocks in the drv_netdev_read function module and no longer executes to avoid invalid query operations when idle; when the kernel is ready to send a packet, the kernel-state program calls the drv_netdev_hard_xmit function module, adds the skb to be sent to the kernel packet list skb_list, and wakes up the drv_netdev_read function blocked in the waiting queue tx_wait to enable it to continue to execute; and takes out an skb from the kernel packet list skb_list, and reassembles it into the format of the user-state message, and copies the newly assembled user-state message to the memory space pointed to by pkb through the copy_to_user function, thereby completing this data transmission.
[0045] The present invention is based on the above technical solution. The present invention is mainly used for a system and method for efficiently transmitting message data between kernel state and user state. When the kernel does not send a message, the user state packet receiving process is blocked, which avoids a large number of invalid query operations, reduces CPU occupancy and is relatively easy to implement with high reliability. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] The present disclosure may be better understood by describing exemplary embodiments of the present disclosure in conjunction with the accompanying drawings, in which:
[0047] Figure 1 A system for exchanging messages between kernel mode and user mode according to an embodiment of the present disclosure is shown;
[0048] Figure 2 A flow chart showing a method for transmitting a message from a user state to a kernel state in a system for exchanging messages between a kernel state and a user state according to an embodiment of the present disclosure; and
[0049] Figure 3 A flow chart of a method for transmitting a message from a kernel state to a user state in a system for exchanging messages between a kernel state and a user state according to an embodiment of the present disclosure is shown. DETAILED DESCRIPTION
[0050] The specific implementation methods of the present disclosure will be described below. It should be noted that in the specific description of these implementation methods, in order to provide a concise description, it is impossible for this specification to provide a detailed description of all the features of the actual implementation methods. It should be understood that in the actual implementation of any implementation method, just as in the process of any engineering project or design project, in order to achieve the specific goals of the developer and to meet system-related or business-related restrictions, various specific decisions are often made, and this will also change from one implementation method to another. In addition, it can also be understood that although the efforts made in this development process may be complex and lengthy, for ordinary technicians in the field related to the content disclosed by the present disclosure, some changes in design, manufacturing or production based on the technical content disclosed in the present disclosure are just conventional technical means, and should not be understood as insufficient content of the present disclosure.
[0051] Unless otherwise defined, the technical or scientific terms used in the claims and the specification shall have the usual meaning understood by persons with ordinary skills in the technical field to which the present disclosure belongs. The words "first", "second" and similar words used in the patent application specification and the claims of the present disclosure do not indicate any order, quantity or importance, but are only used to distinguish different components. Words such as "one" or "a" do not indicate a quantitative limitation, but rather indicate the presence of at least one. Words such as "include" or "comprise" and similar words mean that the elements or objects appearing before "include" or "comprises" cover the elements or objects listed after "include" or "comprises" and their equivalent elements, and do not exclude other elements or objects.
[0052] According to an embodiment of the present disclosure, in a Linux operating system, when a process executes a system call and enters the kernel code for execution, we call the process in kernel operation, or simply kernel state.
[0053] According to an embodiment of the present disclosure, also in the Linux operating system, when a process is executing the user's own code (non-system call and other functions), it is said to be in user mode.
[0054] According to an embodiment of the present disclosure, the Proc file system is a special file system created by software, which provides an interface for accessing system kernel data in the form of a file system. This file system enables information exchange between the kernel space and the user space.
[0055] According to an embodiment of the present disclosure, a socket buffer (skb) is a Linux network core data structure, which represents a message to be sent or processed and runs through the entire protocol stack.
[0056] The Linux operating system uses the file_operations structure to associate system calls with drivers. Each member of this structure has a name corresponding to a call. The corresponding function pointer in file_operation (such as the read or write function module) is read, and then the control is transferred to the function module, thus completing the work of the Linux device driver.
[0057] refer to Figure 1 , which shows a system for exchanging messages between kernel state and user state according to an embodiment of the present disclosure. A system for exchanging messages between kernel state and user state, the system comprising: an association creation component 10, used to create a file operation structure 110, so as to associate a system call component with a device driver component, so that each member name in the file operation structure corresponds to a call; an interface creation component 20, used to create an exchange interface 120 between the kernel and the user, so as to execute the operation of the user accessing the system kernel data, so that the kernel space and the user space can exchange information; a message exchange component 40, used to start the exchange interface based on user instructions, and read the corresponding function pointer in the file operation structure by executing the system call of the file operation structure, so as to then transfer the control to the function, thereby completing the device driver.
[0058] The file operation structure 110 is a file_operations structure 110, in which the pointers of the read and write function modules are respectively declared as: ssize_t (*read) (struct file *, char __user *, size_t, loff_t *), ssize_t (*write) (struct file *, const char __user *, size_t, loff_t*), wherein the pointer parameter __user is a buffer for placing information. In the following description, if the file_operations structure and the file operation structure are mentioned, the mark 110 can be used.
[0059] The switching interface 120 is a proc file named channel_net_dev file 120, and its structure is: proc_create("channel_net_dev", 00777, NULL, &g_netdev_fops); after the channel_net_dev file is opened in the user state, the read function module is called according to the channel_net_dev file to transfer data from the kernel space to the user space, and the write function module is called in the user state to transfer data from the user space to the kernel space. In the following description, if the channel_net_dev file and the switching interface are mentioned, the mark 120 can be used.
[0060] The system further comprises a kernel structure creation component 30 for creating a kernel structure, also called a kernel state struct drv_netdev_info_s structure 130. In the following description, the reference 100 may be used when referring to the kernel state struct drv_netdev_info_s structure and the kernel structure.
[0061] The system is part of the Linux operating system and includes a file_operations structure 110, a channel_net_dev file 120, and a struct drv_netdev_info_s structure 130, such as Figure 1 As shown. These structures or files are consistent with the structures or files of the same name in the Linux operating system, so they are not described in detail here. Only the parts of these structures or files related to the present disclosure are mentioned below.
[0062] The pointers to the read and write function modules in the file_operations structure 110 are declared as follows:
[0063] ssize_t (*read) (struct file *, char __user *, size_t, loff_t*);
[0064] ssize_t (*write) (struct file *, const char __user *, size_t, loff_t*);
[0065] Here, the pointer parameter __user is the buffer where the information is placed, that is, the user space memory address. Some data to be transmitted to the kernel can be written into the memory space pointed to by __user. Then, the program (or process) calls the copy_from_user function module in kernel mode (the functions not specially defined here and below are all kernel functions of the Linux operating system) to copy the user state data in the buffer indicated by the pointer parameter __user to the kernel mode according to the file_operations structure.
[0066] The channel_net_dev file is a proc file with the following structure:
[0067] proc_create("channel_net_dev", 00777, NULL,&g_netdev_fops);
[0068] Focus on specifying the read and write functions drv_netdev_read and drv_netdev_write of the channel_net_dev file. The details are as follows:
[0069] const struct file_operations g_netdev_fops =
[0070] {
[0071] .owner = THIS_MODULE,
[0072] .read = drv_netdev_read,
[0073] .write = drv_netdev_write,
[0074] .open = drv_netdev_open,
[0075] .release = drv_netdev_release,
[0076] .unlocked_ioctl = drv_netdev_ioctl,
[0077] };
[0078] After such specification, after the program opens the channel_net_dev file in user mode, it can call the drv_netdev_read in kernel mode by calling the read function module to pass data from kernel space to user space; and when the program calls the write function module in user mode, it can call the drv_netdev_write function module in kernel mode to pass data from user space to kernel space.
[0079] Therefore, with the channel_net_dev file as the medium, information interaction between the kernel state and the user can be completed.
[0080] According to an embodiment of the present disclosure, the user_netdev_write function module can also be implemented using a virtual port. By defining the virtual port in the user state, the process of sending a message from the user state to the kernel state is abstracted into the process of sending a data packet by the virtual port.
[0081] Specifically, a virtual port is defined in user mode, named veth0_2, and its packet sending function port_start_xmit (similar to hard_start_xmit, also a function pointer) is assigned to the user_netdev_write function module.
[0082] In this way, you only need to call the packet sending function of port veth0_2 to call the user_netdev_write function, and then send the message from user state to kernel state.
[0083] The kernel state struct drv_netdev_info_s structure 130 is defined as:
[0084] struct drv_netdev_info_s
[0085] {
[0086] struct sk_buff_head skb_list; / *kernel packet list* /
[0087] wait_queue_head_t tx_wait; / *Packet waiting queue* /
[0088] };
[0089] This structure is used to save the kernel packet list skb_list and the packet waiting queue tx_wait.
[0090] After the system is started and initialized, a process user_netdev_hard_xmit_thread is created in user mode. The process is used to monitor whether the kernel has sent packets through the kernel packet sending list and packet sending waiting queue information saved in the kernel state struct drv_netdev_info_s structure 130. For example, when the kernel packet sending list skb_list is empty, it is determined that the process has no message waiting to be sent.
[0091] The process user_netdev_hard_xmit_thread opens the channel_net_dev file in a blocking manner, and repeatedly calls the read function module in an infinite loop, and then calls the kernel state drv_netdev_read function module. Each time the drv_netdev_read function module is entered, the current process is added to the waiting queue tx_wait.
[0092] In addition, before calling the read function, the program will take out a piece of memory space from a memory pool in user mode and pass the first address pkb of the memory space to drv_netdev_read through the second parameter __user of the read function.
[0093] When the kernel does not send a packet (that is, the kernel packet list skb_list is empty), the program will be blocked in the drv_netdev_read function (execute __set_current_state(TASK_INTERRUPTIBLE) and schedule()), and will not execute further, thus avoiding a large number of invalid query operations during idle time.
[0094] When the kernel is ready to send a packet, the kernel-mode program calls the drv_netdev_hard_xmit function to add the skb to be sent to the kernel packet list skb_list. The function then wakes up the process blocked in the waiting queue tx_wait. After being awakened, the drv_netdev_read function continues to execute, takes out an skb from the kernel packet list skb_list, and reassembles it into the format of the user-mode message. The newly assembled user-mode message is copied to the memory space pointed to by pkb through the copy_to_user function module to complete the data transmission.
[0095] According to an embodiment of the present disclosure, the drv_netdev_hard_xmit function module can also be implemented using a virtual port. By defining a virtual port in kernel state, the process of sending a message from kernel state to user state is abstracted into the process of sending a data packet by the virtual port.
[0096] Specifically, in the Linux system, network devices are abstracted as struct net_device structures, and each network device has a unique device name.
[0097] Define a virtual port in kernel state, name it ieth0_1, and assign its packet sending function hard_start_xmit (a function pointer defined in the struct net_device structure) to the drv_netdev_hard_xmit function module.
[0098] The function prototype of hard_start_xmit is as follows:
[0099] int (*hard_start_xmit) (struct sk_buff * skb, struct net_device *dev);
[0100] Among them, sk_buff is the structure used to send data packets. In this way, you only need to call the packet sending function of port ieth0_1 to call the drv_netdev_hard_xmit function module, and then send the message from kernel state to user state.
[0101] Figure 2 A flow chart of a method for transmitting a message from a user state to a kernel state in a system for exchanging messages between a kernel state and a user state according to an embodiment of the present disclosure is shown. When data is ready to be sent from a user state to a kernel state, a user state program calls a user_netdev_write function module. After opening a channel_net_dev file, the function module calls the kernel state's drv_netdev_write through the write function module, and copies the user state data to the kernel state through the passed-in __user pointer and the copy_from_user function, completing the data transmission.
[0102] Figure 3A flow chart of a method for transmitting a message from kernel state to user state in a system for exchanging messages between kernel state and user state according to an embodiment of the present disclosure is shown. After the system is started and initialized, the program creates a process user_netdev_hard_xmit_thread in user state. This process opens the channel_net_dev file in a blocking manner, and repeatedly calls the read function module in an infinite loop, and then calls the drv_netdev_read function module in kernel state. Each time the drv_netdev_read function module is entered, the current process will be added to the waiting queue tx_wait.
[0103] In addition, before calling the read function module, the program will take out a piece of memory space from a memory pool in user mode and pass the first address pkb of the memory space to drv_netdev_read through the second parameter __user of the read function module.
[0104] When the kernel does not send a packet (that is, the kernel packet list skb_list is empty), the program will be blocked in the drv_netdev_read function (execute __set_current_state(TASK_INTERRUPTIBLE) and schedule()), and will not execute further, thus avoiding a large number of invalid query operations during idle time.
[0105] When the kernel is ready to send a packet, the kernel-mode program calls the drv_netdev_hard_xmit function to add the skb to be sent to the kernel packet list skb_list. The function then wakes up the process blocked in the waiting queue tx_wait. After being awakened, the drv_netdev_read function continues to execute, takes out an skb from the kernel packet list skb_list, and reassembles it into the format of the user-mode message. The newly assembled user-mode message is copied to the memory space pointed to by pkb through the copy_to_user function to complete the data transmission.
[0106] The present invention is based on the above technical solution. The present invention is mainly used for a system and method for efficiently transmitting message data between kernel state and user state. When the kernel does not send a message, the user state packet receiving process is blocked, which avoids a large number of invalid query operations, reduces CPU occupancy and is relatively easy to implement with high reliability.
[0107] The basic principles of the present disclosure are described above in conjunction with specific embodiments. However, it should be pointed out that a person skilled in the art can understand that all or any steps or components of the method and system of the present disclosure can be implemented in hardware, firmware, software or a combination thereof in any computing device (including a processor, storage medium, etc.) or a network of computing devices. This can be achieved by a person skilled in the art using their basic programming skills after reading the description of the present disclosure.
[0108] Therefore, the purpose of the present disclosure can also be achieved by running a program or a group of programs on any computing device. The computing device can be a well-known general-purpose device. Therefore, the purpose of the present disclosure can also be achieved by simply providing a program product containing a program code that implements the method or system. In other words, such a program product also constitutes the present disclosure, and a storage medium storing such a program product also constitutes the present disclosure. Obviously, the storage medium can be any well-known storage medium or any storage medium developed in the future.
[0109] It should also be noted that in the system and method of the present disclosure, it is obvious that each component or each step can be decomposed and / or recombined. These decompositions and / or recombinations should be regarded as equivalent schemes of the present disclosure. In addition, the steps of performing the above series of processes can naturally be performed in chronological order according to the order of description, but do not necessarily need to be performed in chronological order. Some steps can be performed in parallel or independently of each other.
[0110] The above specific implementations do not constitute a limitation on the protection scope of the present disclosure. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present disclosure shall be included in the protection scope of the present disclosure.
Claims
1. A system for exchanging messages between kernel state and user state, the system comprising: An association creation component is used to create a file operation structure to associate the system call component with the device driver component, so that each member name in the file operation structure corresponds to a call; The interface creation component is used to create an exchange interface between the kernel and the user, so as to perform the operation of the user accessing the system kernel data, so that the kernel space and the user space can exchange information. The exchange interface is a proc file named channel_net_dev file, and its structure is: proc_create("channel_net_dev", 00777, NULL, &g_netdev_fops), the channel_net_dev file specifies the read and write functions drv_netdev_read and drv_netdev_write as follows: const struct file_operations g_netdev_fops = { .owner = THIS_MODULE, .read = drv_netdev_read, .write = drv_netdev_write, .open = drv_netdev_open, .release = drv_netdev_release, .unlocked_ioctl = drv_netdev_ioctl, }; Thus, after opening the channel_net_dev file in the user state, the read function is called to call the kernel state drv_netdev_read to transfer data from the kernel space to the user space, and the kernel state drv_netdev_write function module is called by calling the write function module to transfer data from the user space to the kernel space; The message exchange component is used to start the exchange interface based on the user instruction, read the corresponding function pointer in the file operation structure by executing the system call of the file operation structure, so as to then transfer the control right to the function, thereby completing the device driver, The kernel structure creation component is used to create the kernel state struct drv_netdev_info_s structure of the following structure: struct drv_netdev_info_s { struct sk_buff_head skb_list; wait_queue_head_t tx_wait; }; This kernel state structure is used to save the kernel packet list skb_list and the packet waiting queue tx_wait. After the system is started and initialized, a user_netdev_hard_xmit_thread process is created in the user state. The process is used to monitor whether the kernel has sent a packet through the kernel packet sending list and packet sending waiting queue information saved in the kernel state struct drv_netdev_info_s structure. The user_netdev_hard_xmit_thread process opens the channel_net_dev file in a blocking manner, and repeatedly calls the read function module in an infinite loop, and then calls the drv_netdev_read function module in the kernel state. Each time the drv_netdev_read function module in the kernel state is called, the current process is added to the waiting queue tx_wait. Before calling the read function module, the process takes out a piece of memory space from a memory pool in the user state, and passes the first address pkb of the memory space to drv_netdev_read through the second parameter __user of the read function. When the kernel does not send a packet, the process is blocked in the drv_netdev_read function module and no longer executes.
2. The system according to claim 1, wherein the file operation structure is a file_operations structure, and the pointer declarations of the read and write function modules in the structure are respectively: ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); The pointer parameter __user is the buffer where the information is stored.
3. The system according to claim 1, wherein after the channel_net_dev file is opened in the user state, a read function module is called according to the channel_net_dev file to transfer data from the kernel space to the user space, and a write function module is called in the user state to transfer data from the user space to the kernel space.
4. The system according to claim 1, wherein the write function module calls the copy_from_user function module in kernel mode to copy the user mode data in the buffer indicated by the pointer parameter __user to the kernel mode according to the file_operations structure.
5. The system according to claim 1, wherein when data is ready to be sent from user state to kernel state, a user_netdev_write function module is called in user state, wherein the user_netdev_write function module is assigned to the packet sending function port_start_xmit of the first virtual port, so that the packet sending function of the first virtual port is called and the message is sent from user state to kernel state.
6. The system according to claim 1, wherein when the kernel is ready to send a packet, the socket cache skb to be sent is added to the kernel packet sending list skb_list, and the blocked process in the waiting queue tx_wait is awakened to allow the drv_netdev_read function to continue to execute, an skb is taken out from the kernel packet sending list skb_list, and reassembled into the format of the user mode message, and the newly assembled user mode message is copied to the memory space pointed to by pkb through the copy_to_user function module to complete this data transmission.
7. The system according to claim 6, wherein the drv_netdev_hard_xmit function module is assigned to the packet sending function hard_start_xmit of the second virtual port, and the function prototype of the hard_start_xmit is as follows: int (*hard_start_xmit) (struct sk_buff * skb, struct net_device * dev); Among them, sk_buff is a structure used to send data packets, so as to call the drv_netdev_hard_xmit function module by calling the packet sending function of the second virtual port.
8. A method for transmitting a message from a user state to a kernel state in a system according to any one of claims 1 to 7, comprising the following steps: When data is ready to be sent from user state to kernel state, the user_netdev_write function module is called in user state; as well as After the user_netdev_write function opens the channel_net_dev file, it calls the kernel-state drv_netdev_write through the write function, and copies the user-state data to the kernel-state through the passed-in __user pointer and the copy_from_user function to complete the data transmission.
9. A method for transmitting a message from a kernel state to a user state in a system according to any one of claims 1 to 7, comprising the following steps: After the system is started and initialized, a process user_netdev_hard_xmit_thread is created in the user state, which opens the channel_net_dev file created by the kernel state channel_net_dev creation module in the kernel state in a blocking manner, and repeatedly calls the read function in an infinite loop, and then calls the kernel state drv_netdev_read function. Each time the drv_netdev_read function is entered, the current process is added to the waiting queue tx_wait, so as to monitor whether the kernel has sent a packet according to the kernel packet sending list skb_list. Before calling the read function module, the program takes a piece of memory space from a memory pool in the user state, and passes the first address pkb of the memory space to drv_netdev_read through the second parameter __user of the read function module; When the kernel does not send a packet, it blocks in the drv_netdev_read function module and does not execute further; When the kernel is ready to send a packet, the kernel-mode program calls the drv_netdev_hard_xmit function module, adds the skb to be sent to the kernel packet list skb_list, and wakes up the drv_netdev_read function blocked in the waiting queue tx_wait to enable it to continue executing; and Take out an skb from the kernel packet list skb_list and reassemble it into the format of the user-mode message. Use the copy_to_user function to copy the newly assembled user-mode message to the memory space pointed to by pkb, thereby completing this data transmission.
Citation Information
Patent Citations
Air interface data cipher acceleration method of wireless communication base station
CN104102494A
Networking system call data division for zero copy operations
US20110270944A1
