A method and device for processing a two-layer protocol message based on an embedded Linux system
By converting Layer 2 protocol messages into UDP data packets for sending and receiving, the complexity of Layer 2 protocol development in embedded Linux systems is solved, enabling more efficient product development and system stability.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- NANJING GUODIAN NANZI POWER GRID AUTOMATION CO LTD
- Filing Date
- 2023-01-06
- Publication Date
- 2026-04-14
AI Technical Summary
Implementing Layer 2 protocol functionality in existing embedded Linux systems requires modifications to the kernel code, leading to complex development, vulnerability risks, and impacting system stability.
The sending and receiving of Layer 2 protocol messages are converted into the sending and receiving of UDP data messages, and the process is carried out by creating sockets and UDP ports, which reduces the development difficulty.
It simplifies the development process of Layer 2 protocol modules, improving product development efficiency and system stability.
Smart Images

Figure CN116156015B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to a method and apparatus for processing Layer 2 protocol messages based on an embedded Linux system, belonging to the field of computer technology. Background Technology
[0002] Embedded systems are now widely used in various fields of our lives, such as mobile phones, set-top boxes, digital TVs, digital cameras, handheld PDAs, mobile computing devices, home automation systems, switches, routers, consumer electronics, industrial automation instruments, and medical instruments. The most important part of an embedded system is its embedded operating system. Common embedded systems include Linux, UCOS, Windows CE, VxWorks, and FreeRTOS. Embedded Linux is characterized by its free licensing, excellent performance, easy software portability, open-source code, widespread application software support, short application development cycles, rapid new product launches, and readily available code for reference and porting. It also boasts good real-time performance, stability, and security. Therefore, more and more projects are choosing Linux as their embedded operating system.
[0003] Some network-related embedded systems, such as switches and routers, sometimes need to support Layer 2 protocol functions, such as LLDP (Link Layer Discovery Protocol) and STP (Spanning Tree Protocol). When an embedded Linux system needs to implement these Layer 2 protocol functions, it must be able to receive and send Layer 2 protocol messages. The common approach is to modify the Linux kernel code, register a new network protocol, add processing for that protocol's messages to the Linux protocol stack, recompile the Linux kernel, and then create a socket for that network protocol in the application to receive and send Layer 2 protocol messages. This method involves Linux kernel programming, is relatively complex to implement, and places high demands on the network protocol code. If the network protocol code has vulnerabilities, it can cause problems for the entire Linux system at runtime. Summary of the Invention
[0004] The purpose of this invention is to overcome the shortcomings of existing technologies and provide a Layer 2 protocol message processing method and apparatus based on an embedded Linux system. This method can convert the sending and receiving of Layer 2 protocol messages into the sending and receiving of UDP data packets, thereby reducing the development difficulty of Layer 2 protocol modules and accelerating product development. To achieve the above objective, this invention employs the following technical solution:
[0005] In a first aspect, the present invention provides a Layer 2 protocol message processing method based on an embedded Linux system, comprising: a sending thread and a receiving thread.
[0006] The sending thread includes: receiving Layer 2 protocol messages from the network interface, and sending the Layer 2 protocol messages as UDP data packets to the corresponding UDP port according to the destination MAC address of the Layer 2 protocol messages;
[0007] The receiving thread includes: receiving Layer 2 protocol messages in the form of UDP data packets sent by the Layer 2 protocol module from the UDP port, and sending the Layer 2 protocol messages in the form of UDP data packets to the network interface.
[0008] In conjunction with the first aspect, further, the step of obtaining Layer 2 protocol messages from the network interface includes: creating a raw socket, setting the socket to promiscuous mode, and receiving Layer 2 protocol messages from the network interface.
[0009] In conjunction with the first aspect, further, the step of sending the Layer 2 protocol message as a UDP datagram to the corresponding UDP port based on the destination MAC address of the Layer 2 protocol message includes:
[0010] Determine if the destination MAC address of the Layer 2 protocol message is the same as the MAC address of Layer 2 protocol module A:
[0011] If so, the Layer 2 protocol message will be sent as the data portion of the UDP datagram to UDP port 10001;
[0012] If not, determine whether the destination MAC address of the Layer 2 protocol message is the MAC address of the Layer 2 protocol B module. If yes, send the Layer 2 protocol message as the data part of the UDP data packet to UDP port 10002. If not, ignore the Layer 2 protocol message.
[0013] In conjunction with the first aspect, the sending thread further includes: Layer 2 protocol A module receiving UDP data packets from UDP port 10001, and Layer 2 protocol B module receiving UDP data packets from UDP port 10002.
[0014] In conjunction with the first aspect, the receiving thread further includes: all Layer 2 protocol modules that need to send data send the Layer 2 protocol messages to be sent to UDP port 10000 in the form of UDP data packets.
[0015] In conjunction with the first aspect, the step of receiving Layer 2 protocol messages in the form of UDP data packets sent by the Layer 2 protocol module from the UDP port further includes: creating a datagram socket, binding the datagram socket to the UDP port 10000, and receiving Layer 2 protocol messages from the UDP port 10000.
[0016] In conjunction with the first aspect, further, sending a Layer 2 protocol message in the form of a UDP datagram to the network interface includes: sending the data portion of the UDP datagram as a message to the network interface.
[0017] Secondly, this invention provides a Layer 2 protocol message processing device based on an embedded Linux system, comprising: a sending thread processing module and a receiving thread processing module.
[0018] The sending thread processing module includes:
[0019] The first acquisition submodule is used to receive Layer 2 protocol messages from the network interface.
[0020] First sending submodule: Used to send the Layer 2 protocol message as a UDP data packet to the corresponding UDP port according to the destination MAC address of the Layer 2 protocol message;
[0021] The receiving thread processing module includes:
[0022] The second acquisition submodule is used to receive Layer 2 protocol messages in the form of UDP datagrams sent by the Layer 2 protocol module from the UDP port.
[0023] The second sending submodule is used to send Layer 2 protocol messages in UDP datagram format to the network interface.
[0024] Thirdly, the present invention provides a computer device, including a processor and a storage medium;
[0025] The storage medium is used to store instructions;
[0026] The processor is configured to operate according to the instructions to perform the steps of the method according to the first aspect.
[0027] Fourthly, this aspect provides a computer-readable storage medium having a computer program stored thereon that, when executed by a processor, implements the steps of the method described in the first aspect.
[0028] Compared with the prior art, the beneficial effects achieved by the Layer 2 protocol message processing method and apparatus based on an embedded Linux system provided in this embodiment of the invention include:
[0029] This invention provides a Layer 2 protocol message processing method, comprising: a sending thread and a receiving thread. The sending thread includes: receiving Layer 2 protocol messages from a network interface, and sending the Layer 2 protocol messages in UDP datagram format to a corresponding UDP port according to the destination MAC address of the Layer 2 protocol messages. The receiving thread includes: receiving Layer 2 protocol messages in UDP datagram format sent by a Layer 2 protocol module from a UDP port, and sending the UDP datagram-formatted Layer 2 protocol messages to the network interface. When a Layer 2 protocol module needs to send Layer 2 protocol messages, it sends the messages to the UDP port; when it needs to receive Layer 2 protocol messages, it receives them from the corresponding UDP port. This invention converts the sending and receiving of Layer 2 protocol messages into the sending and receiving of UDP datagrams, which can reduce the development difficulty of Layer 2 protocol modules and thus accelerate the product development process. Attached Figure Description
[0030] Figure 1 This is a flowchart of a Layer 2 protocol message processing method provided in Embodiment 1 of the present invention;
[0031] Figure 2 This is a flowchart of the sending thread in a Layer 2 protocol message processing method provided in Embodiment 1 of the present invention;
[0032] Figure 3 This is a flowchart of the receiving thread in a Layer 2 protocol message processing method provided in Embodiment 1 of the present invention. Detailed Implementation
[0033] The present invention will be further described below with reference to the accompanying drawings. The following embodiments are only used to more clearly illustrate the technical solution of the present invention, and should not be used to limit the scope of protection of the present invention.
[0034] Example 1:
[0035] A Layer 2 protocol message processing method based on an embedded Linux system includes a sending thread and a receiving thread.
[0036] like Figure 1 As shown, the sending thread includes:
[0037] Step 1: Obtain Layer 2 protocol messages from the network interface.
[0038] By creating a raw socket, the socket is set to promiscuous mode, which means it receives all packets from the network interface, including packets not intended for the local machine.
[0039] Step 2: Based on the destination MAC address of the Layer 2 protocol message, send the Layer 2 protocol message as a UDP datagram to the corresponding UDP port.
[0040] Determine if the destination MAC address of the Layer 2 protocol message is the same as the MAC address of Layer 2 protocol module A:
[0041] If so, the Layer 2 protocol message should be sent to the Layer 2 Protocol A module for processing, and the Layer 2 protocol message should be sent as the data part of the UDP data packet to UDP port 10001.
[0042] If not, then determine whether the destination MAC address of the Layer 2 protocol message is the MAC address of the Layer 2 protocol B module:
[0043] If so, the Layer 2 protocol message should be sent to the Layer 2 Protocol B module for processing, and the Layer 2 protocol message should be sent as the data part of the UDP data packet to UDP port 10002.
[0044] If not, it means that the Layer 2 protocol message is neither a message from Layer 2 protocol module A nor a message from Layer 2 protocol module B, and the Layer 2 protocol message is ignored.
[0045] Continue to retrieve the next Layer 2 protocol message from the network interface.
[0046] Step 3: Layer 2 protocol module A receives UDP data packets from UDP port 10001, and Layer 2 protocol module B receives UDP data packets from UDP port 10002.
[0047] like Figure 2 As shown, the receiving thread includes:
[0048] Step 1: Receive Layer 2 protocol messages in the form of UDP data packets sent by the Layer 2 protocol module from the UDP port.
[0049] All Layer 2 protocol modules that need to send data will send the Layer 2 protocol messages to be sent to UDP port 10000 in the form of UDP datagrams.
[0050] Create a datagram socket, bind the datagram socket to UDP port 10000, and receive Layer 2 protocol messages from UDP port 10000.
[0051] Step 2: Send the Layer 2 protocol message in UDP datagram format to the network interface.
[0052] The data portion of the Layer 2 protocol message on the UDP port is sent as a single message to the network interface.
[0053] The UDP port continues to receive the next UDP datagram from the Layer 2 protocol module.
[0054] like Figure 3The diagram shows an overall flowchart of a Layer 2 protocol message processing method based on an embedded Linux system. Thread 1 is the sending thread. If the Layer 2 protocol message belongs to module A, it is sent as the data portion of a UDP datagram to UDP port 10001. If the Layer 2 protocol message belongs to module B, it is sent as the data portion of a UDP datagram to UDP port 10002. Module A receives UDP datagrams from UDP port 10001, and module B receives UDP datagrams from UDP port 10002. The data portion of the UDP datagram is the original Layer 2 protocol message.
[0055] Thread 2 is the receiving thread. All Layer 2 protocol modules that need to send data will send the Layer 2 protocol messages to be sent as UDP datagrams to UDP port 10000. Thread 2 receives UDP datagrams from UDP port 10000, and after receiving the UDP datagrams, treats the data portion of the message as a data packet and sends it to the network interface.
[0056] This embodiment converts the sending and receiving of Layer 2 protocol messages into the sending and receiving of UDP data packets, which can reduce the development difficulty of Layer 2 protocol modules and thus accelerate the product development process.
[0057] Example 2:
[0058] This invention provides a Layer 2 protocol message processing device based on an embedded Linux system, comprising: a sending thread processing module and a receiving thread processing module.
[0059] The sending thread processing module includes:
[0060] The first acquisition submodule is used to receive Layer 2 protocol messages from the network interface.
[0061] First sending submodule: Used to send the Layer 2 protocol message as a UDP data packet to the corresponding UDP port according to the destination MAC address of the Layer 2 protocol message;
[0062] The receiving thread processing module includes:
[0063] The second acquisition submodule is used to receive Layer 2 protocol messages in the form of UDP datagrams sent by the Layer 2 protocol module from the UDP port.
[0064] The second sending submodule is used to send Layer 2 protocol messages in UDP datagram format to the network interface.
[0065] Example 3:
[0066] This invention provides a computer device equipped with an embedded Linux system, including a processor and a storage medium;
[0067] The storage medium is used to store instructions;
[0068] The processor is configured to operate according to the instructions to execute the steps of the method described in Embodiment 1.
[0069] Example 4:
[0070] This invention provides a computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements the steps of the method described in Embodiment 1.
[0071] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0072] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0073] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0074] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0075] The above description is only a preferred embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the technical principles of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. A method for processing Layer 2 protocol messages based on an embedded Linux system, characterized in that, include: Sending thread and receiving thread, The sending thread includes: receiving Layer 2 protocol messages from the network interface, and sending the Layer 2 protocol messages as UDP data packets to the corresponding UDP port according to the destination MAC address of the Layer 2 protocol messages; The step of obtaining Layer 2 protocol messages from the network interface includes: creating a raw socket, setting the socket to promiscuous mode, and receiving Layer 2 protocol messages from the network interface. The step of sending the Layer 2 protocol message as a UDP datagram to the corresponding UDP port according to the destination MAC address of the Layer 2 protocol message includes: Determine if the destination MAC address of the Layer 2 protocol message is the same as the MAC address of Layer 2 protocol module A: If so, the Layer 2 protocol message will be sent as the data portion of the UDP datagram to UDP port 10001; If not, determine whether the destination MAC address of the Layer 2 protocol message is the MAC address of the Layer 2 protocol B module. If yes, send the Layer 2 protocol message as the data part of the UDP data packet to UDP port 10002. If not, ignore the Layer 2 protocol message. The receiving thread includes: receiving Layer 2 protocol messages in the form of UDP data packets sent by the Layer 2 protocol module from the UDP port, and sending the Layer 2 protocol messages in the form of UDP data packets to the network interface; The step of receiving Layer 2 protocol messages in the form of UDP data packets sent by the Layer 2 protocol module from the UDP port includes: creating a datagram socket, binding the datagram socket to UDP port 10000, and receiving Layer 2 protocol messages from UDP port 10000. The receiving thread also includes: all Layer 2 protocol modules that need to send data will send the Layer 2 protocol messages to be sent to UDP port 10000 in the form of UDP data packets.
2. The Layer 2 protocol message processing method based on an embedded Linux system according to claim 1, characterized in that, The sending thread further includes: Layer 2 protocol A module receiving UDP data packets from UDP port 10001, and Layer 2 protocol B module receiving UDP data packets from UDP port 10002.
3. The Layer 2 protocol message processing method based on an embedded Linux system according to claim 1, characterized in that, Sending a Layer 2 protocol message in UDP datagram format to the network interface includes: sending the data portion of the UDP datagram as a message to the network interface.
4. A Layer 2 protocol message processing device based on an embedded Linux system, characterized in that, include: Send thread processing module and receive thread processing module, The sending thread processing module includes: The first acquisition submodule is used to receive Layer 2 protocol messages from the network interface. The acquisition of Layer 2 protocol messages from the network interface includes: creating a raw socket, setting the socket to promiscuous mode, and receiving Layer 2 protocol messages from the network interface. The first sending submodule is used to send the Layer 2 protocol message in UDP datagram format to the corresponding UDP port according to the destination MAC address of the Layer 2 protocol message; wherein, sending the Layer 2 protocol message in UDP datagram format to the corresponding UDP port according to the destination MAC address of the Layer 2 protocol message includes: Determine if the destination MAC address of the Layer 2 protocol message is the same as the MAC address of Layer 2 protocol module A: If so, the Layer 2 protocol message will be sent as the data portion of the UDP datagram to UDP port 10001; If not, determine whether the destination MAC address of the Layer 2 protocol message is the MAC address of the Layer 2 protocol B module. If yes, send the Layer 2 protocol message as the data part of the UDP data packet to UDP port 10002. If not, ignore the Layer 2 protocol message. The receiving thread processing module includes: The second acquisition submodule is used to receive Layer 2 protocol messages in the form of UDP data packets sent by Layer 2 protocol modules from the UDP port. The process of receiving Layer 2 protocol messages in the form of UDP data packets from the Layer 2 protocol modules from the UDP port includes: creating a datagram socket, binding the datagram socket to the UDP port 10000, and receiving Layer 2 protocol messages from the UDP port 10000; it also includes: all Layer 2 protocol modules that need to send data sending their Layer 2 protocol messages to be sent to the UDP port 10000 in the form of UDP data packets. The second sending submodule is used to send Layer 2 protocol messages in UDP datagram format to the network interface.
5. A computer device, characterized in that, Including processor and storage media; The storage medium is used to store instructions; The processor is configured to operate according to the instructions to perform the steps of the method according to any one of claims 1 to 3.
6. A computer-readable storage medium having a computer program stored thereon, characterized in that, When executed by a processor, the program implements the steps of the method according to any one of claims 1 to 3.
Citation Information
Patent Citations
Analysis method and device of two-layer message based on vxWorks operation system
CN106101123A