A method for initializing multiple network interfaces of an embedded device and a terminal
By using the bootloader and kernel processing of the embedded system, the problem of multiple network ports having the same MAC address in embedded devices was solved, thus ensuring the uniqueness of each network port's MAC address and the stability of services.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- X-SPEED INFORMATION TECH CO LTD
- Filing Date
- 2020-12-10
- Publication Date
- 2026-06-26
AI Technical Summary
In embedded devices, the same MAC address across multiple network ports can cause service anomalies. Existing technologies cannot guarantee that the MAC addresses of each network port are different and stable.
The embedded system's bootloader reads the pre-stored MAC address, writes it into the dtb file, and when the kernel loads the dsa driver, it obtains the MAC base address, performs preset processing, and obtains consecutive and distinct network port MAC addresses, which are then written into the corresponding network port structure for initialization.
Ensuring that each network port has a unique MAC address guarantees the stability and normal operation of the device's services.
Smart Images

Figure CN116781671B_ABST
Abstract
Description
[0001] This case is a divisional application based on the invention patent filed on December 10, 2020, with application number 202011451052.2 and titled "A Method and Terminal for Allocating MAC Addresses for Multiple Network Ports in an Embedded Device". Technical Field
[0002] This invention relates to the field of computer technology, and in particular to a method and terminal for initializing multiple network ports of an embedded device. Background Technology
[0003] In VPN (Virtual Private Network) and network security devices, a large number of network ports need to be supported. Due to limited CPU resources, switch chips are used to expand the number of network ports. Since switch chips use DSA drivers (a driver framework that materializes the various interfaces of the switch chip), the network ports of the switch chip become independent physical network ports. However, in this process, the MAC addresses (Media Access Control Addresses, also known as LAN addresses, Ethernet addresses, and physical addresses) of all network ports are the same as the uplink MAC addresses, which does not meet product requirements. If user-space programs are used to manually set the MAC addresses to each network port, individual service anomalies may occur. Summary of the Invention
[0004] The technical problem to be solved by the present invention is to provide a method and terminal for allocating MAC addresses of multiple network ports of an embedded device, so that the MAC addresses of the multiple network ports of the embedded device are different while ensuring service stability.
[0005] To solve the above-mentioned technical problems, the technical solution adopted by the present invention is as follows:
[0006] A method for allocating MAC addresses across multiple network ports in an embedded device includes:
[0007] S1. Start the bootloader of the embedded system, read the pre-stored MAC address, and write the MAC address into the dtb file;
[0008] S2. Start the kernel. When loading the dsa driver, obtain the MAC address in the dtb file as the MAC base address. Perform preset processing on the MAC base address to obtain continuous, distinct network port MAC addresses with the same number as the number of network ports. Write each network port MAC address into the structure of the corresponding network port and initialize the network port.
[0009] S3, Start the file system.
[0010] A method for initializing multiple network ports in an embedded device, comprising the following steps:
[0011] S1. Start the bootloader of the embedded system, read the pre-stored MAC address, and write the MAC address into the dtb file;
[0012] Specifically, the MAC base address is written into the uplink port location where the switch chip used by the embedded device connects to the CPU in the dtb file;
[0013] S2. Start the kernel. When loading the dsa driver, obtain the MAC address in the dtb file as the MAC base address. Perform preset processing on the MAC base address to obtain continuous, distinct network port MAC addresses with the same number as the number of network ports. Write each of the network port MAC addresses into the structure of the corresponding network port to initialize the network port.
[0014] To solve the above-mentioned technical problems, another technical solution adopted by the present invention is as follows:
[0015] An embedded device multi-port MAC address allocation terminal includes a processor, a memory, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it performs the following steps:
[0016] S1. Start the bootloader of the embedded system, read the pre-stored MAC address, and write the MAC address into the dtb file;
[0017] S2. Start the kernel. When loading the dsa driver, obtain the MAC address in the dtb file as the MAC base address. Perform preset processing on the MAC base address to obtain continuous, distinct network port MAC addresses with the same number as the number of network ports. Write each network port MAC address into the structure of the corresponding network port and initialize the network port.
[0018] S3, Start the file system.
[0019] An embedded device multi-network port initialization terminal includes a processor, a memory, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it performs the following steps:
[0020] S1. Start the bootloader of the embedded system, read the pre-stored MAC address, and write the MAC address into the dtb file;
[0021] Specifically, the MAC base address is written into the uplink port location where the switch chip used by the embedded device connects to the CPU in the dtb file;
[0022] S2. Start the kernel. When loading the dsa driver, obtain the MAC address in the dtb file as the MAC base address. Perform preset processing on the MAC base address to obtain continuous, distinct network port MAC addresses with the same number as the number of network ports. Write each of the network port MAC addresses into the structure of the corresponding network port to initialize the network port.
[0023] The beneficial effects of this invention are as follows: This invention transmits the MAC base address to the kernel through the bootloader of the embedded system, and the kernel processes the base address to ensure that the MAC address allocated to each network port is different. Furthermore, the kernel writes the MAC address into the structure of each network port, that is, the address is allocated to each network port in kernel mode for subsequent network port initialization, ensuring that each network port is in a normal state and can perform business processing normally. Attached Figure Description
[0024] Figure 1 This is a flowchart illustrating a method for allocating MAC addresses across multiple network ports in an embedded device, according to an embodiment of the present invention.
[0025] Figure 2 This is a structural diagram of an embedded device multi-port MAC address allocation terminal according to an embodiment of the present invention;
[0026] Figure 3 This is a detailed flowchart of a method for allocating MAC addresses across multiple network ports in an embedded device, according to an embodiment of the present invention.
[0027] Figure 4 This is a schematic diagram illustrating a step result of an embedded device multi-port MAC address allocation method according to an embodiment of the present invention;
[0028] Figure 5 This is a schematic diagram illustrating the running results of a method for allocating MAC addresses across multiple network ports in an embedded device according to an embodiment of the present invention.
[0029] Label Explanation:
[0030] 1. An embedded device multi-port MAC address allocation terminal; 2. Processor; 3. Memory. Detailed Implementation
[0031] To explain in detail the technical content, objectives, and effects of the present invention, the following description is provided in conjunction with the embodiments and accompanying drawings.
[0032] Please refer to Figure 1 as well as Figure 3 A method for allocating MAC addresses for multiple network ports in an embedded device, comprising:
[0033] S1. Start the bootloader of the embedded system, read the pre-stored MAC address, and write the MAC address into the dtb file;
[0034] S2. Start the kernel. When loading the dsa driver, obtain the MAC address in the dtb file as the MAC base address. Perform preset processing on the MAC base address to obtain continuous, distinct network port MAC addresses with the same number as the number of network ports. Write each network port MAC address into the structure of the corresponding network port and initialize the network port.
[0035] S3, Start the file system.
[0036] As can be seen from the above description, the beneficial effects of the present invention are as follows: The present invention transmits the MAC base address to the kernel through the bootloader of the embedded system, and the kernel processes the base address to ensure that the MAC address allocated to each network port is different. The kernel writes the MAC address into the structure of each network port, that is, the address is allocated to each network port in kernel mode for subsequent network port initialization, ensuring that each network port is in a normal state and can perform business processing normally.
[0037] Furthermore, the procedure before step S1 includes:
[0038] S01. Obtain the MAC address of the current device's network card and store it as an environment variable in the boot1 partition.
[0039] As described above, the optimal location for storing MAC addresses is one that is difficult for users to operate, not easy to lose, and easy to read. Therefore, the MAC addresses are stored in the boot1 partition. Since the bootloader of the embedded system uses the boot0 partition, and the source code of the bootloader of the embedded system also has a tool for reading the environment variables of the boot in user mode, it is convenient and accurate to write the MAC addresses to the environment variables of the boot1 partition, which is the optimal location.
[0040] Furthermore, step S1 specifically includes:
[0041] S11. Start the bootloader of the embedded system, read the first MAC address of the boot1 partition and the second MAC address of the boot0 partition respectively, compare whether the first MAC address and the second MAC address are the same. If they are not the same, write the first MAC address of the boot1 partition as the second MAC address to the environment variable of the boot0 partition, and proceed to step S12. Otherwise, proceed directly to step S12.
[0042] S12. When the bootloader parses the dtb file, it writes the second MAC address in the boot0 partition into the dtb file.
[0043] As described above, before writing the second MAC address in the boot0 partition to the dtb file, the present invention needs to compare the second MAC address with the MAC address in boot1 to ensure that the second MAC address in boot0 is real-time and correct.
[0044] Furthermore, the preset processing of the MAC base address in step S2 specifically involves:
[0045] The MAC base address is incremented.
[0046] As can be seen from the above description, the present invention increments the MAC address, which is simple to operate and can quickly obtain consecutive and different MAC addresses for allocation.
[0047] Furthermore, the bootloader for starting the embedded system in step S1 is uboot.
[0048] As can be seen from the above description, the bootloader for starting the embedded system is uboot, which is a specific embodiment of the present invention.
[0049] Please refer to Figure 2 An embedded device multi-port MAC address allocation terminal includes a processor, a memory, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it performs the following steps:
[0050] S1. Start the bootloader of the embedded system, read the pre-stored MAC address, and write the MAC address into the dtb file;
[0051] S2. Start the kernel. When loading the dsa driver, obtain the MAC address in the dtb file as the MAC base address. Perform preset processing on the MAC base address to obtain continuous, distinct network port MAC addresses with the same number as the number of network ports. Write each network port MAC address into the structure of the corresponding network port and initialize the network port.
[0052] S3, Start the file system.
[0053] As can be seen from the above description, the beneficial effects of the present invention are as follows: The present invention transmits the MAC base address to the kernel through the bootloader of the embedded system, and the kernel processes the base address to ensure that the MAC address allocated to each network port is different. The kernel writes the MAC address into the structure of each network port, that is, the address is allocated to each network port in kernel mode for subsequent network port initialization, ensuring that each network port is in a normal state and can perform business processing normally.
[0054] Furthermore, before step S1, the processor executes the computer program, and the following steps are included:
[0055] S01. Obtain the MAC address of the current device's network card and store it as an environment variable in the boot1 partition.
[0056] As described above, the optimal location for storing MAC addresses is one that is difficult for users to operate, not easy to lose, and easy to read. Therefore, the MAC addresses are stored in the boot1 partition. Since the bootloader of the embedded system uses the boot0 partition, and the source code of the bootloader of the embedded system also has a tool for reading the environment variables of the boot in user mode, it is convenient and accurate to write the MAC addresses to the environment variables of the boot1 partition, which is the optimal location.
[0057] Furthermore, step S1 specifically includes:
[0058] S11. Start the bootloader of the embedded system, read the first MAC address of the boot1 partition and the second MAC address of the boot0 partition respectively, compare whether the first MAC address and the second MAC address are the same. If they are not the same, write the first MAC address of the boot1 partition as the second MAC address to the environment variable of the boot0 partition, and proceed to step S12. Otherwise, proceed directly to step S12.
[0059] S12. When the bootloader parses the dtb file, it writes the second MAC address in the boot0 partition into the dtb file.
[0060] As described above, before writing the second MAC address in the boot0 partition to the dtb file, the present invention needs to compare the second MAC address with the MAC address in boot1 to ensure that the second MAC address in boot0 is real-time and correct.
[0061] Furthermore, the preset processing of the MAC base address in step S2 specifically involves:
[0062] The MAC base address is incremented.
[0063] As can be seen from the above description, the present invention increments the MAC address, which is simple to operate and can quickly obtain consecutive and different MAC addresses for allocation.
[0064] Furthermore, the bootloader for starting the embedded system in step S1 is uboot.
[0065] As can be seen from the above description, the bootloader for starting the embedded system is uboot, which is a specific embodiment of the present invention.
[0066] Please refer to Figure 1 , Figure 3 , Figure 4 as well as Figure 5 Embodiment 1 of the present invention is as follows:
[0067] A method for allocating MAC addresses across multiple network ports in an embedded device includes:
[0068] S01. Obtain the MAC address of the current device's network card and store it as an environment variable in the boot1 partition;
[0069] In this embodiment, since the MAC address is best stored in a location that is not easily operated by the user, is not easily lost, and is easy to read, the MAC address is stored in the boot1 partition. Because uboot uses the boot0 partition, and the uboot source code also has a tool for reading the environment variables of the boot partition in user space, it is convenient to write accurately. Writing the MAC address to the environment variables of the boot1 partition is the optimal location.
[0070] S1. Start the bootloader of the embedded system, read the pre-stored MAC address, and write the MAC address into the dtb file;
[0071] Step S1 specifically involves:
[0072] S11. Start the bootloader of the embedded system, read the first MAC address of the boot1 partition and the second MAC address of the boot0 partition respectively, compare whether the first MAC address and the second MAC address are the same. If they are not the same, write the first MAC address of the boot1 partition as the second MAC address to the environment variable of the boot0 partition, and proceed to step S12. Otherwise, proceed directly to step S12.
[0073] S12. When the bootloader parses the dtb file, it writes the second MAC address in the boot0 partition into the dtb file;
[0074] The bootloader for starting the embedded system in step S1 is uboot;
[0075] In this embodiment, as Figure 4 As shown, uboot executes automatically every time the system boots up. After reading the MAC address in boot1, it reads the MAC address in boot0 and compares the two MAC addresses. If they are the same, it returns directly. If they are different, it overwrites the MAC address in boot1 into the boot0 environment variable. At this time, the MAC address in the boot0 partition is the same as the MAC address in the boot1 partition.
[0076] After the MAC address is written to the boot0 environment, uboot needs to write the MAC address settings to the dtb file when parsing the dtb file so that it can be passed to the kernel. We usually write the MAC base address to the uplink port location where the switch chip used by the device connects to the CPU in the dtb file. The dtb file is a device tree file.
[0077] S2. Start the kernel. When loading the dsa driver, obtain the MAC address in the dtb file as the MAC base address. Perform preset processing on the MAC base address to obtain continuous, distinct network port MAC addresses with the same number as the number of network ports. Write each network port MAC address into the structure of the corresponding network port and initialize the network port.
[0078] The specific steps for pre-setting the MAC base address in step S2 are as follows:
[0079] Increment the MAC base address;
[0080] S3, Start the file system.
[0081] In this embodiment, when the kernel boots up and loads the DSA driver, it obtains the MAC address passed from the DTB file. Through the DSA driver, when each switch port is materialized, the MAC address is copied to the corresponding port. Since there is only one base address, the driver increments the MAC address based on the device's switch chip and the port being used, and then saves it to the port's structure. The final result after system boot is as follows: Figure 5 As shown.
[0082] Please refer to Figure 2 Embodiment two of the present invention is as follows:
[0083] An embedded device multi-port MAC address allocation terminal 1 includes a processor 2, a memory 3, and a computer program stored in the memory 3 and executable on the processor 2. When the processor 2 executes the computer program, it implements the steps in Embodiment 1 above.
[0084] In summary, the present invention provides a method and terminal for allocating MAC addresses for multiple network ports in an embedded device. The present invention transmits the MAC base address to the kernel through the embedded system's bootloader, and the kernel processes the base address, ensuring that the MAC address allocated to each network port is different. Furthermore, the kernel writes the MAC address into the structure of each network port, meaning that address allocation for each network port is performed in kernel mode for subsequent network port initialization, ensuring that each network port is in a normal state and can perform normal service processing. Moreover, the present invention verifies the MAC address when reading it, ensuring the correctness of the MAC address.
[0085] The above description is merely an embodiment of the present invention and does not limit the patent scope of the present invention. Any equivalent modifications made based on the content of the present invention specification and drawings, or direct or indirect applications in related technical fields, are similarly included within the patent protection scope of the present invention.
Claims
1. A method for initializing multiple network ports in an embedded device, characterized in that, Including the following steps: S01. Obtain the MAC address of the current device's network card and store it as an environment variable in the boot1 partition; S1. Start the bootloader of the embedded system, read the pre-stored MAC address, and write the MAC address into the dtb file, including: S11. Start the bootloader of the embedded system, read the first MAC address of the boot1 partition and the second MAC address of the boot0 partition respectively, compare whether the first MAC address and the second MAC address are the same. If they are not the same, write the first MAC address of the boot1 partition as the second MAC address to the environment variable of the boot0 partition, and proceed to step S12. Otherwise, proceed directly to step S12. S12. When the bootloader parses the dtb file, it writes the second MAC address in the boot0 partition into the dtb file; Specifically, the MAC base address is written into the uplink port location where the switch chip used by the embedded device connects to the CPU in the dtb file; S2. Start the kernel. When loading the dsa driver, obtain the MAC address in the dtb file as the MAC base address. Perform preset processing on the MAC base address to obtain continuous, distinct network port MAC addresses with the same number as the number of network ports. Write each of the network port MAC addresses into the structure of the corresponding network port to initialize the network port.
2. The embedded device multi-network port initialization method according to claim 1, characterized in that, The specific steps for pre-setting the MAC base address in step S2 are as follows: The MAC base address is incremented.
3. The embedded device multi-network port initialization method according to claim 1, characterized in that, The bootloader for starting the embedded system in step S1 is uboot.
4. An embedded device multi-port initialization terminal, comprising a processor, a memory, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it performs the following steps: S01. Obtain the MAC address of the current device's network card and store it as an environment variable in the boot1 partition; S1. Start the bootloader of the embedded system, read the pre-stored MAC address, and write the MAC address into the dtb file, including: S11. Start the bootloader of the embedded system, read the first MAC address of the boot1 partition and the second MAC address of the boot0 partition respectively, compare whether the first MAC address and the second MAC address are the same. If they are not the same, write the first MAC address of the boot1 partition as the second MAC address to the environment variable of the boot0 partition, and proceed to step S12. Otherwise, proceed directly to step S12. S12. When the bootloader parses the dtb file, it writes the second MAC address in the boot0 partition into the dtb file; Specifically, the MAC base address is written into the uplink port location where the switch chip used by the embedded device connects to the CPU in the dtb file; S2. Start the kernel. When loading the dsa driver, obtain the MAC address in the dtb file as the MAC base address. Perform preset processing on the MAC base address to obtain continuous, distinct network port MAC addresses with the same number as the number of network ports. Write each of the network port MAC addresses into the structure of the corresponding network port to initialize the network port.
5. An embedded device multi-network port initialization terminal according to claim 4, characterized in that, The specific steps for pre-setting the MAC base address in step S2 are as follows: The MAC base address is incremented.
6. The embedded device multi-port initialization terminal according to claim 4, characterized in that, The bootloader for starting the embedded system in step S1 is uboot.