Disclosure of Invention
The invention mainly aims to provide a message forwarding method, a terminal and a computer readable storage medium, and aims to solve the problem of low message forwarding speed in the prior art.
In order to achieve the above object, the present invention provides a packet forwarding method, which includes the following steps:
applying for a section of memory in the kernel as a shared memory, and sharing the shared memory by the application layer and the kernel;
a forwarding application program configures a forwarding rule and sends the forwarding rule to the shared memory for storage;
and the network port driver receives a message sent by network port hardware, reads a forwarding rule from the shared memory, matches the forwarding rule of the current message, and calls the network port to forward the current message.
Optionally, the message forwarding method, where the applying for a section of memory in the kernel as a shared memory and the application layer and the kernel share the shared memory, specifically includes:
starting a kernel forwarding module;
applying for continuous physical memory;
setting a memory page where the physical memory is located as a reserved page mark;
remapping the physical memory to a user space;
and the kernel rule table is a memory space pointed by the physical memory.
Optionally, in the packet forwarding method, the user space represents an address space.
Optionally, the message forwarding method, where the forwarding application configures a forwarding rule, and sends the forwarding rule to the shared memory for storage, specifically includes:
opening a user space APP;
opening a driving device of the kernel forwarding module;
acquiring a forwarding rule table address;
initializing a forwarding rule table;
and allocating a forwarding rule to each port number according to the UDP port number as an index.
Optionally, the message forwarding method, wherein the forwarding rule includes: a source MAC, a destination MAC, and a valid flag.
Optionally, the message forwarding method includes that the portal driver receives a message sent by portal hardware, reads a forwarding rule from a shared memory, matches the forwarding rule of the current message, and calls a portal to forward the current message, where the method specifically includes:
the first network port starts to receive the message;
judging whether the message is an IP message or not;
if the message is an IP message, judging whether the message is a UDP message or not;
if the message is a UDP message, judging whether the UDP destination port number is larger than a preset value;
if the UDP destination port number is larger than the preset value, reading a forwarding rule corresponding to the port number;
replacing the destination NAC of the message with a destination MAC in the forwarding rule;
replacing the source MAC of the message with the source MAC of the second network port;
and calling a port function to forward the message to the second network port.
Optionally, the message forwarding method includes that the portal driver receives a message sent by portal hardware, reads a forwarding rule from a shared memory, matches the forwarding rule of the current message, and calls a portal to forward the current message, where the method specifically includes:
the second network port starts to receive the message;
judging whether the message is an IP message or not;
if the message is an IP message, judging whether the message is a UDP message or not;
if the message is a UDP message, judging whether the UDP destination port number is larger than a preset value;
if the UDP destination port number is larger than the preset value, reading a forwarding rule corresponding to the port number;
replacing the destination NAC of the message with a destination MAC in the forwarding rule;
replacing the source MAC of the message with the source MAC of the first internet access;
and calling a port function to forward the message to the first internet access.
Optionally, the method for forwarding a packet, wherein the determining whether the packet is an IP packet, further includes:
if the message is not an IP message, the message is uploaded to a protocol stack;
the judging whether the message is a UDP message further includes:
if the message is not a UDP message, uploading the message to a protocol stack;
if the message is a UDP message, judging whether the UDP destination port number is larger than a preset value, and then:
if the UDP destination port number is not larger than the preset value, the message is uploaded to a protocol stack.
In addition, to achieve the above object, the present invention further provides a terminal, wherein the terminal includes: the message forwarding program realizes the steps of the message forwarding method when being executed by the processor.
In addition, to achieve the above object, the present invention further provides a computer readable storage medium, wherein the computer readable storage medium stores a message forwarding program, and the message forwarding program implements the steps of the message forwarding method when executed by a processor.
The method comprises the steps that a section of memory is applied to a kernel to serve as a shared memory, and an application layer and the kernel share the shared memory; a forwarding application program configures a forwarding rule and sends the forwarding rule to the shared memory for storage; and the network port driver receives a message sent by network port hardware, reads a forwarding rule from the shared memory, matches the forwarding rule of the current message, and calls the network port to forward the current message. The invention directly transmits the message on the network port drive, the message is only processed on the network port drive, the application layer and the kernel drive share the memory and are used for storing the transmission rule issued by the application layer, when a message is received from the network port drive, the transmission rule is matched to perform corresponding message processing, and then the interface sent by the network port is called to directly transmit the message, thereby greatly improving the transmission performance.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention clearer and clearer, the present invention is further described in detail below with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.
As shown in fig. 4 and 5, the message forwarding method according to the preferred embodiment of the present invention includes the following steps:
step S10, applying for a section of memory in the kernel as a shared memory, and sharing the shared memory by the application layer and the kernel;
step S20, the forwarding application program configures the forwarding rule and sends the forwarding rule to the shared memory for storage;
step S30, the network interface driver receives the message sent by the network interface hardware, and the network interface driver reads the forwarding rule from the shared memory, matches the forwarding rule of the current message, and invokes the network interface to forward the current message.
Specifically, as shown in fig. 6, the kernel forwarding module VPM is started, a continuous physical memory ptr is applied for kmalloc (4M), a memory page where the physical memory ptr is located is set as a reserved page flag, the physical memory ptr is remapped to a user space through remap _ pfn _ range, and the kernel rule table is a memory space pointed to by the physical memory prt.
The user space refers to an address space, such as a 32-bit computer, the address space is 32 powers of 2, that is, 4GB, a 64-bit computer, and the address space is 64 powers of 2. In the Linux system, the address space for running the kernel software and the address space for running the application program are separated and cannot be directly accessed with each other. In order to make the memory of the kernel space accessible to the APP user process, the memory needs to be remapped to the user APP space by mmap ().
Specifically, as shown in fig. 7, the user space APP is started, the driver fd of the kernel forwarding module VPM is opened ("/dev/VPM"), the forwarding rule table address m _ pstpvmtable is obtained as mmap (fd), the forwarding rule table m _ pstpvmtable is initialized to all 0, one forwarding rule is allocated to each port number according to the UDP port number as an index, and 1 ten thousand rules are totalized. Wherein the forwarding rule comprises: a source MAC, a destination MAC, and a valid flag.
Specifically, as shown in fig. 8, the first network port (eth0) starts receiving the packet, determines the type of the packet from eth0, determines the type of the packet to be 0x800, i.e. whether it is an IP packet, if it is not, the packet is handed over to the protocol stack, if the message is an IP message, continuously judging whether the message is a UDP message or not, if the message is not the UDP message, uploading the packet message to a protocol stack, if the message is a UDP message, judging whether the port of the UDP destination port is larger than a preset value, namely, judging whether the UDP destination port number is more than 1000, if the UDP destination port number is not more than the preset value, uploading the packet message to a protocol stack, if the destination port number of the UDP is larger than the preset value, reading a forwarding rule corresponding to the port number port, replacing the destination NAC of the message with a destination MAC in the forwarding rule, replacing the source MAC of the message with the source MAC of the second network port (eth1), and calling a port function gfar _ send to forward the message to the second network port eth 1.
Specifically, as shown in fig. 9, the second network port (eth1) starts receiving the packet, determines the type of the packet from eth0, determines the type of the packet to be 0x800, i.e. whether it is an IP packet, if it is not, the packet is handed over to the protocol stack, if the message is an IP message, continuously judging whether the message is a UDP message or not, if the message is not the UDP message, uploading the packet message to a protocol stack, if the message is a UDP message, judging whether the port of the UDP destination port is larger than a preset value, namely, judging whether the UDP destination port number is more than 1000, if the UDP destination port number is not more than the preset value, uploading the packet message to a protocol stack, if the destination port number of the UDP is larger than the preset value, reading a forwarding rule corresponding to the port number port, replacing the destination NAC of the message with a destination MAC in the forwarding rule, replacing the source MAC of the message with a source MAC of the internet access (eth0), and calling a port function gfar _ send to forward the message to the first internet access eth 0.
Through practical tests, the scheme of the invention can achieve 15 ten thousand bidirectional messages per second on the NXP P1021, and does not lose packets.
Further, as shown in fig. 10, based on the message forwarding method and system, the present invention also provides a terminal, which includes aprocessor 10, amemory 20, and adisplay 30. Fig. 10 shows only some of the components of the terminal, but it is to be understood that not all of the shown components are required to be implemented, and that more or fewer components may be implemented instead.
Thememory 20 may in some embodiments be an internal storage unit of the terminal, such as a hard disk or a memory of the terminal. Thememory 20 may also be an external storage device of the terminal in other embodiments, such as a plug-in hard disk, a Smart Media Card (SMC), a Secure Digital (SD) Card, a Flash memory Card (Flash Card), and the like provided on the terminal. Further, thememory 20 may also include both an internal storage unit and an external storage device of the terminal. Thememory 20 is used for storing application software installed in the terminal and various types of data, such as program codes of the installation terminal. Thememory 20 may also be used to temporarily store data that has been output or is to be output. In one embodiment, thememory 20 stores amessage forwarding program 40, and themessage forwarding program 40 can be executed by theprocessor 10, so as to implement the message forwarding method in the present application.
Theprocessor 10 may be a Central Processing Unit (CPU), a microprocessor or other data Processing chip in some embodiments, and is configured to run program codes stored in thememory 20 or process data, such as executing the message forwarding method.
Thedisplay 30 may be an LED display, a liquid crystal display, a touch-sensitive liquid crystal display, an OLED (Organic Light-Emitting Diode) touch panel, or the like in some embodiments. Thedisplay 30 is used for displaying information at the terminal and for displaying a visual user interface. The components 10-30 of the terminal communicate with each other via a system bus.
In one embodiment, when theprocessor 10 executes themessage forwarding program 40 in thememory 20, the following steps are implemented:
applying for a section of memory in the kernel as a shared memory, and sharing the shared memory by the application layer and the kernel;
a forwarding application program configures a forwarding rule and sends the forwarding rule to the shared memory for storage;
and the network port driver receives a message sent by network port hardware, reads a forwarding rule from the shared memory, matches the forwarding rule of the current message, and calls the network port to forward the current message.
The applying for a section of memory in the kernel as a shared memory, and the sharing of the shared memory by the application layer and the kernel specifically include:
starting a kernel forwarding module;
applying for continuous physical memory;
setting a memory page where the physical memory is located as a reserved page mark;
remapping the physical memory to a user space;
and the kernel rule table is a memory space pointed by the physical memory.
Wherein the user space represents an address space.
The configuring, by the forwarding application program, a forwarding rule, and sending the forwarding rule to the shared memory for storage specifically includes:
opening a user space APP;
opening a driving device of the kernel forwarding module;
acquiring a forwarding rule table address;
initializing a forwarding rule table;
and allocating a forwarding rule to each port number according to the UDP port number as an index.
Wherein the forwarding rule comprises: a source MAC, a destination MAC, and a valid flag.
The method includes that the network interface driver receives a message sent by network interface hardware, reads a forwarding rule from a shared memory, matches the forwarding rule of a current message, and calls a network interface to forward the current message, and specifically includes:
the first network port starts to receive the message;
judging whether the message is an IP message or not;
if the message is an IP message, judging whether the message is a UDP message or not;
if the message is a UDP message, judging whether the UDP destination port number is larger than a preset value;
if the UDP destination port number is larger than the preset value, reading a forwarding rule corresponding to the port number;
replacing the destination NAC of the message with a destination MAC in the forwarding rule;
replacing the source MAC of the message with the source MAC of the second network port;
and calling a port function to forward the message to the second network port.
The method includes that the network interface driver receives a message sent by network interface hardware, reads a forwarding rule from a shared memory, matches the forwarding rule of a current message, and calls a network interface to forward the current message, and specifically includes:
the second network port starts to receive the message;
judging whether the message is an IP message or not;
if the message is an IP message, judging whether the message is a UDP message or not;
if the message is a UDP message, judging whether the UDP destination port number is larger than a preset value;
if the UDP destination port number is larger than the preset value, reading a forwarding rule corresponding to the port number;
replacing the destination NAC of the message with a destination MAC in the forwarding rule;
replacing the source MAC of the message with the source MAC of the first internet access;
and calling a port function to forward the message to the first internet access.
Wherein, the judging whether the message is an IP message further comprises:
if the message is not an IP message, the message is uploaded to a protocol stack;
the judging whether the message is a UDP message further includes:
if the message is not a UDP message, uploading the message to a protocol stack;
if the message is a UDP message, judging whether the UDP destination port number is larger than a preset value, and then:
if the UDP destination port number is not larger than the preset value, the message is uploaded to a protocol stack.
The present invention also provides a computer-readable storage medium, wherein the computer-readable storage medium stores a message forwarding program, and the message forwarding program implements the steps of the message forwarding method when executed by a processor.
In summary, the present invention provides a message forwarding method, a terminal and a computer-readable storage medium, where the method includes: applying for a section of memory in the kernel as a shared memory, and sharing the shared memory by the application layer and the kernel; a forwarding application program configures a forwarding rule and sends the forwarding rule to the shared memory for storage; and the network port driver receives a message sent by network port hardware, reads a forwarding rule from the shared memory, matches the forwarding rule of the current message, and calls the network port to forward the current message. The invention directly transmits the message on the network port drive, the message is only processed on the network port drive, the application layer and the kernel drive share the memory and are used for storing the transmission rule issued by the application layer, when a message is received from the network port drive, the transmission rule is matched to perform corresponding message processing, and then the interface sent by the network port is called to directly transmit the message, thereby greatly improving the transmission performance.
It should be noted that, in this document, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or terminal that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or terminal. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other like elements in a process, method, article, or terminal that comprises the element.
Of course, it will be understood by those skilled in the art that all or part of the processes of the methods of the embodiments described above can be implemented by instructing relevant hardware (such as a processor, a controller, etc.) through a computer program, and the program can be stored in a computer readable storage medium, and when executed, the program can include the processes of the embodiments of the methods described above. The computer readable storage medium may be a memory, a magnetic disk, an optical disk, etc.
It is to be understood that the invention is not limited to the examples described above, but that modifications and variations may be effected thereto by those of ordinary skill in the art in light of the foregoing description, and that all such modifications and variations are intended to be within the scope of the invention as defined by the appended claims.