A port monitoring method, device, apparatus and medium
Patent Information
- Application Number
- CN202410053757.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-01-12
- Publication Date
- 2026-10-09
- Estimated Expiration
- 2044-01-12
AI Technical Summary
在传统的套接字编程中,一个套接字实例通常只能监听一个端口,而每个套接字实例都会占用一定的系统资源,包括内存和处理能力,那么当需要监听大量端口时,创建多个套接字实例可能会导致资源消耗过高
[0039] As can be seen, this application obtains network data packets from clients requesting services from target application components via a server; when each network data packet is passed to the transport layer, a preset socket in the server is used to listen to the target port information in the network data packet, and a target distribution program pre-injected in the preset socket is run; wherein, the target distribution program is a distribution program pre-injected into the preset socket using a detector built based on an extended Berkeley packet filter; the target distribution program selects a target port and a target socket that match the target port information from a first memory and a second memory with pre-established association, respectively, to transmit the network data packet using the target port and the target socket; wherein, the first memory is pre-injected with the ports that the target application component needs to listen to and records the correspondence between the ports and the corresponding socket addresses, and the second memory pre-stores the socket handle of the target application component obtained by the detector.
Smart Images

Figure CN117692359B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a port monitoring method, apparatus, device, and medium. Background Technology
[0002] Container technology is a virtualization technology that is more computationally efficient and flexible than virtual machine technology. With the development of container technology, many container orchestration engines have emerged for managing containers. In the cloud era, applications under distributed microservice architectures are becoming increasingly diverse, and the number of services running on a single host node is also increasing. In traditional socket programming, a socket instance typically listens on only one port, and each socket instance consumes certain system resources, including memory and processing power. Therefore, when listening on a large number of ports, creating multiple socket instances can lead to excessive resource consumption. Furthermore, each socket instance requires independent configuration, binding, and management. Using multiple sockets to listen on multiple ports significantly increases code complexity and management difficulty, resulting in substantial manpower and time costs for enterprises.
[0003] In summary, how to provide a more efficient port listening method to reduce resource consumption and lower the complexity and management difficulty of writing code to listen to multiple ports are problems that need to be solved. Summary of the Invention
[0004] In view of this, the purpose of this invention is to provide a port monitoring method, apparatus, device, and medium that can provide a more efficient port monitoring method, thereby reducing resource consumption and lowering the complexity and management difficulty of writing code to monitor multiple ports. The specific solution is as follows:
[0005] Firstly, this application discloses a port listening method applied to a server, including:
[0006] Obtain the network packets used by the client to request services from the target application components;
[0007] When each network data packet is passed to the transport layer, the target port information in the network data packet is listened to using a preset socket in the server, and the target distribution program pre-injected in the preset socket is run; wherein, the target distribution program is a distribution program pre-injected into the preset socket using a detector built based on the extended Berkeley packet filter;
[0008] The target distribution program selects a target port and a target socket that match the target port information from a first memory and a second memory that have been pre-associated, respectively, so as to transmit the network data packets using the target port and the target socket; wherein, the first memory is pre-injected with the ports that the target application component needs to listen to and records the correspondence between the ports and the corresponding socket addresses, and the second memory pre-stores the socket handles of the target application component obtained by the detector.
[0009] Optionally, the step of using the target distribution program to select a target port and a target socket that match the target port information from a first memory and a second memory with pre-established associations includes:
[0010] The target dispatch program is used to search the first memory for a target port and target socket address that match the target port information;
[0011] Search for the target socket address that matches the target socket address from the socket handles stored in the second memory.
[0012] Optionally, before obtaining the network data packets used by the client to request services from the target application component, the method further includes:
[0013] A detector is built based on an extended Berkeley package filter and then loaded into the kernel.
[0014] The first memory and the second memory are loaded into the kernel, and an association is established between the detector and the second memory;
[0015] The target distribution program is injected into the preset socket using a detector built based on the extended Berkeley packet filter;
[0016] The socket handle of the target application component is obtained using a detector built based on the extended Berkeley packet filter, and the socket handle is stored in the second memory based on the association relationship;
[0017] According to the preset configuration information, a number of ports that the target application component needs to listen to are obtained, and the ports are injected into the first memory using a port injector, and a correspondence between the ports and socket addresses is established in the first memory.
[0018] Accordingly, the step of using the target dispatch program to search for the target port and target socket address that match the target port information from the first memory includes:
[0019] The target dispatch program is used to search for a target port in the first memory that matches the target port information, and the target socket address corresponding to the target port is determined based on the correspondence.
[0020] Optionally, the injection of the target distribution program into the preset socket using the detector built based on the extended Berkeley packet filter includes:
[0021] Obtain the namespace to which the preset socket belongs;
[0022] The target distributor is injected into the file handle of the namespace using a detector built based on the extended Berkeley package filter.
[0023] Optionally, obtaining the network data packets used by the client to request services from the target application component includes:
[0024] Use the physical network interface card to obtain the network data packets used by the client to request services from the target application components;
[0025] Each network data packet is transmitted from the physical network card to the kernel protocol stack based on a preset interrupt method; the kernel protocol stack includes the application layer, the transport layer, the network layer, the data link layer, and the physical layer.
[0026] Optionally, the step of transmitting each network data packet from the physical network card to the kernel protocol stack based on a preset interrupt method includes:
[0027] The network data packets are transmitted from the physical network card to the kernel protocol stack based on hard interrupts;
[0028] Alternatively, each network data packet can be transmitted from the physical network card to the kernel protocol stack based on a soft interrupt.
[0029] Optionally, the port listening method further includes:
[0030] The target application component is pre-deployed on a preset container orchestration platform to provide external services, so that the client can request the services of the target application component.
[0031] Secondly, this application discloses a port monitoring device, comprising:
[0032] The data packet acquisition module is used to acquire the network data packets used by the client to request services from the target application components;
[0033] The program execution module is used to listen to the target port information in each network data packet when it is passed to the transport layer using a preset socket in the server, and to run a target distribution program pre-injected in the preset socket; wherein, the target distribution program is a distribution program pre-injected into the preset socket using a detector built based on the extended Berkeley packet filter;
[0034] The matching module is used to select a target port and a target socket that match the target port information from a first memory and a second memory that have been pre-assigned, so as to transmit the network data packets using the target port and the target socket. The first memory is pre-injected with the ports that the target application component needs to listen to and records the correspondence between the ports and the corresponding socket addresses. The second memory pre-stores the socket handles of the target application component obtained by the detector.
[0035] Thirdly, this application discloses an electronic device, comprising:
[0036] Memory, used to store computer programs;
[0037] A processor for executing the computer program to implement the steps of the aforementioned disclosed port listening method.
[0038] Fourthly, this application discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, it implements the steps of the aforementioned disclosed port listening method.
[0039] As can be seen, this application obtains network data packets from clients requesting services from target application components via a server; when each network data packet is passed to the transport layer, a preset socket in the server is used to listen to the target port information in the network data packet, and a target distribution program pre-injected in the preset socket is run; wherein, the target distribution program is a distribution program pre-injected into the preset socket using a detector built based on an extended Berkeley packet filter; the target distribution program selects a target port and a target socket that match the target port information from a first memory and a second memory with pre-established association, respectively, to transmit the network data packet using the target port and the target socket; wherein, the first memory is pre-injected with the ports that the target application component needs to listen to and records the correspondence between the ports and the corresponding socket addresses, and the second memory pre-stores the socket handle of the target application component obtained by the detector.
[0040] The beneficial effects of this application are as follows: The method of this application is applied to a server. The server obtains network data packets used by the client to request services from the target application component. When each network data packet is passed to the transport layer, the server uses a preset socket to listen for the target port information in each network data packet and runs a target distribution program pre-injected in the preset socket. It can be understood that the target distribution program is a distribution program pre-injected into the preset socket using a detector built based on an extended Berkeley packet filter. The server runs the target distribution program to select target ports and target sockets that match the target port information from a first memory and a second memory with pre-established associations to transmit network data packets. That is, this application can listen for port information in multiple network data packets using a preset socket and select matching sockets and ports to transmit multiple network data packets by running the distribution program. In this way, this application can listen for multiple ports using a single socket, achieving a more efficient port listening method, reducing resource consumption, and lowering the complexity and management difficulty of writing code to listen for multiple ports. Attached Figure Description
[0041] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.
[0042] Figure 1 This is a flowchart of a port listening method disclosed in this application;
[0043] Figure 2 This is a flowchart of a specific port listening method disclosed in this application;
[0044] Figure 3 This is a schematic diagram illustrating a single socket that listens to multiple ports, as disclosed in this application.
[0045] Figure 4 This is a schematic diagram of a port monitoring device disclosed in this application;
[0046] Figure 5 This is a structural diagram of an electronic device disclosed in this application. Detailed Implementation
[0047] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of the present invention.
[0048] In traditional socket programming, a socket instance typically listens on only one port, and each socket instance consumes system resources, including memory and processing power. Therefore, when listening on a large number of ports, creating multiple socket instances can lead to excessive resource consumption. Furthermore, each socket instance requires independent configuration, binding, and management; using multiple sockets to listen on multiple ports significantly increases code complexity and management difficulty. To address this, this application discloses a port listening method, apparatus, device, and medium that provides a more efficient port listening approach, reducing resource consumption and lowering the complexity and management difficulty of writing code to listen on multiple ports.
[0049] See Figure 1 As shown in the figure, this application discloses a port listening method applied to a server, the method including:
[0050] Step S11: Obtain the network data packets used by the client to request services from the target application components.
[0051] In this embodiment, the client requests the service of the application component and generates access traffic. Therefore, the server can obtain the network data packets used by the client to request the service of the target application component.
[0052] It should be noted that the above method also includes: pre-deploying the target application component for providing external services on a preset container orchestration platform, so that the client can request the services of the target application component. That is, this embodiment requires pre-deploying the target application component for providing external services on a preset container orchestration platform so that the client can request the services of the target application component. Specifically, the container orchestration platform can be Kubernetes (K8s, an open-source container orchestration engine from Google that supports automated deployment, large-scale scalability, and application containerization management). In other words, this application specifically deploys the target application component in Kubernetes to provide external services.
[0053] Step S12: When each network data packet is passed to the transport layer, the target port information in the network data packet is listened to using a preset socket in the server, and the target distribution program pre-injected in the preset socket is run; wherein, the target distribution program is a distribution program pre-injected into the preset socket using a detector built based on the extended Berkeley packet filter.
[0054] In this embodiment, when each network data packet is passed to the transport layer, the server uses a preset socket to listen for the target port information in each network data packet and runs the target distribution program pre-injected in the preset socket. It can be understood that the target distribution program is a distribution program pre-injected into the preset socket using a detector built based on the extended Berkeley packet filter. When the network data packet passed to the transport layer searches for the listening TCP (Transmission Control Protocol) socket, the injected target distribution program will run. Here, the TCP socket specifically refers to the listening socket.
[0055] Step S13: Using the target distribution program, select a target port and a target socket that match the target port information from the first memory and the second memory, which have been pre-associated, respectively, so as to transmit the network data packet using the target port and the target socket; wherein, the first memory is pre-injected with the ports that the target application component needs to listen to and records the correspondence between the ports and the corresponding socket addresses, and the second memory pre-stores the socket handle of the target application component obtained by the detector.
[0056] In this embodiment, the server runs a target distribution program to select target ports and target sockets that match the target port information from a first memory and a second memory with pre-established associations to transmit network data packets. That is, this application can listen to port information in multiple network data packets using a preset socket and transmit multiple network data packets by running a distribution program to select matching sockets and ports. In this way, this application can listen to multiple ports using a single socket, achieving a more efficient port listening method, reducing resource consumption, and lowering the complexity and management difficulty of writing code to listen to multiple ports.
[0057] As can be seen, this application obtains network data packets from clients requesting services from target application components via a server; when each network data packet is passed to the transport layer, a preset socket in the server is used to listen to the target port information in the network data packet, and a target distribution program pre-injected in the preset socket is run; wherein, the target distribution program is a distribution program pre-injected into the preset socket using a detector built based on an extended Berkeley packet filter; the target distribution program selects a target port and a target socket that match the target port information from a first memory and a second memory with pre-established association, respectively, to transmit the network data packet using the target port and the target socket; wherein, the first memory is pre-injected with the ports that the target application component needs to listen to and records the correspondence between the ports and the corresponding socket addresses, and the second memory pre-stores the socket handle of the target application component obtained by the detector.
[0058] The beneficial effects of this application are as follows: The method of this application is applied to a server. The server obtains network data packets used by the client to request services from the target application component. When each network data packet is passed to the transport layer, the server uses a preset socket to listen for the target port information in each network data packet and runs a target distribution program pre-injected in the preset socket. It can be understood that the target distribution program is a distribution program pre-injected into the preset socket using a detector built based on an extended Berkeley packet filter. The server runs the target distribution program to select target ports and target sockets that match the target port information from a first memory and a second memory with pre-established associations to transmit network data packets. That is, this application can listen for port information in multiple network data packets using a preset socket and select matching sockets and ports to transmit multiple network data packets by running the distribution program. In this way, this application can listen for multiple ports using a single socket, achieving a more efficient port listening method, reducing resource consumption, and lowering the complexity and management difficulty of writing code to listen for multiple ports.
[0059] See Figure 2 As shown, this application discloses a specific port monitoring method. Compared with the previous embodiment, this embodiment further explains and optimizes the technical solution. Specifically, it includes:
[0060] Step S21: Use the physical network card to obtain the network data packets used by the client to request services from the target application components.
[0061] In this embodiment, network data packets first enter the physical network interface card (NIC). That is, the server uses the physical NIC to obtain the network data packets used by the client to request services from the target application components.
[0062] Step S22: Transmit each network data packet from the physical network card to the kernel protocol stack based on a preset interrupt method; the kernel protocol stack includes the application layer, the transport layer, the network layer, the data link layer, and the physical layer.
[0063] In this embodiment, network data packets enter the kernel protocol stack from the physical network card through a preset interrupt method. It should be noted that the kernel protocol stack includes the application layer, transport layer, network layer, data link layer, and physical layer.
[0064] In a specific implementation, the above-mentioned transmission of each network data packet from the physical network card to the kernel protocol stack based on a preset interrupt method includes: transmitting each network data packet from the physical network card to the kernel protocol stack based on a hardware interrupt; or, transmitting each network data packet from the physical network card to the kernel protocol stack based on a software interrupt. That is, network data packets enter the kernel protocol stack from the physical network card via a hardware interrupt or a software interrupt.
[0065] Step S23: When each network data packet is passed to the transport layer, the target port information in the network data packet is listened to using a preset socket in the server, and the target distribution program pre-injected in the preset socket is run; wherein, the target distribution program is a distribution program pre-injected into the preset socket using a detector built based on the extended Berkeley packet filter.
[0066] In this embodiment, it should be noted that before obtaining the network data packets used by the client to request services from the target application component using the physical network card, the method further includes: constructing a detector based on the extended Berkeley packet filter and loading the detector into the kernel; loading the first memory and the second memory into the kernel and establishing an association between the detector and the second memory; injecting the target distribution program into the preset socket using the detector constructed based on the extended Berkeley packet filter; obtaining the socket handle of the target application component using the detector constructed based on the extended Berkeley packet filter, and storing the socket handle in the second memory based on the association; obtaining a number of ports that the target application component needs to listen to according to preset configuration information, injecting the ports into the first memory using a port injector, and establishing a correspondence between the ports and socket addresses in the first memory.
[0067] It is understood that the pre-processing required in this application embodiment includes: constructing a detector based on the extended Berkeley Packet Filter (eBPF) and loading the detector into the kernel; loading the first memory and the second memory into the kernel and establishing an association between the detector and the second memory; furthermore, injecting the target distribution program into a preset socket using the detector built on the extended Berkeley Packet Filter, and obtaining the socket handle of the target application component using the detector built on the extended Berkeley Packet Filter, storing the socket handle in the second memory based on the association; obtaining a number of ports that the target application component needs to monitor according to preset configuration information, injecting the ports into the first memory using a port injector, and establishing a correspondence between the ports and socket addresses in the first memory. That is, this application embodiment includes a detector for injecting a distribution program, a second memory for storing socket handles, a port injector for injecting multiple ports, and a first memory for storing ports.
[0068] Specifically, the aforementioned method of injecting the target distribution program into the preset socket using a detector built on an extended Berkeley packet filter includes: obtaining the namespace to which the preset socket belongs; and injecting the target distribution program into the file handle of the namespace using the detector built on an extended Berkeley packet filter. That is, in this embodiment, the target distribution program is injected into the file handle of the namespace to which the preset socket belongs using a detector built on an extended Berkeley packet filter.
[0069] Step S24: Use the target dispatch program to search for the target port and target socket address that match the target port information in the first memory.
[0070] In this embodiment, a target dispatch program is first used to search for a target port and a target socket address that match the target port information in the first memory. In a specific implementation, the above-mentioned method of using the target dispatch program to search for a target port and a target socket address that match the target port information in the first memory includes: using the target dispatch program to search for a target port that matches the target port information in the first memory, and determining the target socket address corresponding to the target port based on the correspondence. That is, the target port that matches the target port information is first searched in the first memory, and since a correspondence between ports and socket addresses is established in the first memory, the target socket address corresponding to the target port can be determined based on the correspondence.
[0071] Step S25: Search for a target socket that matches the target socket address from the socket handles stored in the second memory, so as to transmit the network data packet using the target port and the target socket; wherein, the first memory is pre-injected with the ports that the target application component needs to listen to and records the correspondence between the ports and the corresponding socket addresses, and the second memory pre-stores the socket handles of the target application component obtained by the detector.
[0072] In this embodiment, after finding the target socket address in the first memory, the target socket that matches the target socket address is found in the socket handles stored in the second memory, so as to use the target port and the target socket to transmit network data packets.
[0073] The following is based on Figure 3 The flowchart in the image is used as an example to illustrate the various components involved in this application in detail:
[0074] This application mainly includes target application components and a detector built based on the extended Berkeley packet filter (i.e., Figure 3 eBPF detector in the first memory (i.e.) Figure 3 The BPF hashmap memory), the second memory (i.e. Figure 3 The BPFsockmap memory), port injector, target distributor (i.e. Figure 3 (Distributor in the middle).
[0075] Target application components: Application components deployed in Kubernetes to provide external services;
[0076] eBPF detector: The eBPF-based detector injects target distribution program code into the file handle of the namespace to which the preset socket belongs. When the network data packet entering from the transport layer searches for the listening TCP socket, the injected target distribution program will run. The target distribution program will select the matching socket and port from sockmap and hashmap to send the data packet, and then terminate the kernel's general search process.
[0077] BPF sockmap memory: is a map type in the BPF (Berkeley Packet Filter) program used to store socket records of target application components;
[0078] Port Injector: Injects multiple ports that the target application component needs to listen on into the BPF hashmap storage;
[0079] BPF hashmap storage: It is a map type in BPF programs that stores listening port records. It can efficiently write data from user space through memory-mapped regions without the need for additional kernel memory copying and system calls.
[0080] Distributor: When a network packet is sent from the transport layer to find a listening TCP socket, the injected BPF program runs. The distributor selects a matching socket and port from the sockmap and hashmap to send the packet.
[0081] The specific process includes: loading the eBPF detector into the kernel and injecting the distribution program code; loading the BPF sockmap memory into the kernel and associating it with the eBPF detector; creating a target application component that provides external services in the platform application interface; the eBPF detector obtaining the socket handle of the target application component and storing it in the BPF sockmap memory; injecting multiple ports that the target application component needs to listen on through the port injector according to the configuration and storing them in the BPF hashmap memory; the client requests the application service, generating access traffic, and the data packet enters the kernel protocol stack from the network card through hardware interrupts and software interrupts. When the network data packet entering from the transport layer searches for the listening TCP socket, the injected BPF program will run, and the distribution program will select the matching socket and port from the sockmap and hashmap to transmit the data packet, and then terminate the kernel's general search process.
[0082] In this way, the above scheme, without modifying the kernel program code, injects dispatcher code into the file handle of the socket's namespace using an eBPF-based detector. When network packets entering from the transport layer search for the listening TCP socket, the injected dispatcher runs. The dispatcher selects a matching socket and port from the sockmap and hashmap to transmit the data packet, then terminates the kernel's general search process. This reduces resource consumption, simplifies program design, improves the performance and manageability of the computing system, and greatly saves manpower and time costs, making it highly valuable for application.
[0083] See Figure 4 As shown in the figure, this application discloses a port listening device applied to a server, the device comprising:
[0084] The data packet acquisition module 11 is used to acquire various network data packets used by the client to request services from the target application component;
[0085] The program execution module 12 is used to listen to the target port information in each network data packet when it is passed to the transport layer using a preset socket in the server, and to run a target distribution program pre-injected in the preset socket; wherein, the target distribution program is a distribution program pre-injected into the preset socket using a detector built based on the extended Berkeley packet filter.
[0086] The matching module 13 is used to select a target port and a target socket that match the target port information from a first memory and a second memory that have been pre-established with an association, so as to transmit the network data packet using the target port and the target socket; wherein, the first memory is pre-injected with the ports that the target application component needs to listen to and records the correspondence between the ports and the corresponding socket addresses, and the second memory pre-stores the socket handle of the target application component obtained by the detector.
[0087] As can be seen, this application obtains network data packets from clients requesting services from target application components via a server; when each network data packet is passed to the transport layer, a preset socket in the server is used to listen to the target port information in the network data packet, and a target distribution program pre-injected in the preset socket is run; wherein, the target distribution program is a distribution program pre-injected into the preset socket using a detector built based on an extended Berkeley packet filter; the target distribution program selects a target port and a target socket that match the target port information from a first memory and a second memory with pre-established association, respectively, to transmit the network data packet using the target port and the target socket; wherein, the first memory is pre-injected with the ports that the target application component needs to listen to and records the correspondence between the ports and the corresponding socket addresses, and the second memory pre-stores the socket handle of the target application component obtained by the detector.
[0088] The beneficial effects of this application are as follows: The method of this application is applied to a server. The server obtains network data packets used by the client to request services from the target application component. When each network data packet is passed to the transport layer, the server uses a preset socket to listen for the target port information in each network data packet and runs a target distribution program pre-injected in the preset socket. It can be understood that the target distribution program is a distribution program pre-injected into the preset socket using a detector built based on an extended Berkeley packet filter. The server runs the target distribution program to select target ports and target sockets that match the target port information from a first memory and a second memory with pre-established associations to transmit network data packets. That is, this application can listen for port information in multiple network data packets using a preset socket and select matching sockets and ports to transmit multiple network data packets by running the distribution program. In this way, this application can listen for multiple ports using a single socket, achieving a more efficient port listening method, reducing resource consumption, and lowering the complexity and management difficulty of writing code to listen for multiple ports.
[0089] In some specific embodiments, the matching module 13 may specifically include:
[0090] The first lookup unit is used to use the target distribution program to look up the target port and target socket address that match the target port information from the first memory;
[0091] The second lookup unit is used to search for a target socket that matches the target socket address from the socket handles stored in the second memory.
[0092] In some specific embodiments, before the data packet acquisition module 11, the device further includes:
[0093] The first loading unit is used to build a detector based on the extended Berkeley package filter and load the detector into the kernel;
[0094] The second loading unit is used to load the first memory and the second memory into the kernel, and to establish an association between the detector and the second memory;
[0095] The program injection unit is used to inject the target distribution program into the preset socket using a detector built based on the extended Berkeley packet filter;
[0096] The first storage unit is used to obtain the socket handle of the target application component using a detector built based on the extended Berkeley packet filter, and to store the socket handle in the second memory based on the association relationship.
[0097] The second storage unit is used to obtain a number of ports that the target application component needs to listen to according to preset configuration information, so as to inject the ports into the first memory using a port injector, and establish a correspondence between the ports and socket addresses in the first memory.
[0098] Accordingly, the first search unit is specifically used for:
[0099] The target dispatch program is used to search for a target port in the first memory that matches the target port information, and the target socket address corresponding to the target port is determined based on the correspondence.
[0100] In some specific embodiments, the program injection unit specifically includes:
[0101] The namespace acquisition unit is used to acquire the namespace to which the preset socket belongs;
[0102] The distributor injection unit is used to inject the target distributor into the file handle of the namespace using a detector built based on the extended Berkeley package filter.
[0103] In some specific embodiments, the data packet acquisition module 11 may specifically include:
[0104] The data acquisition unit is used to acquire network data packets used by the client to request services from the target application components using the physical network card;
[0105] The data transmission unit is used to transmit each network data packet from the physical network card to the kernel protocol stack based on a preset interrupt method; the kernel protocol stack includes the application layer, the transport layer, the network layer, the data link layer, and the physical layer.
[0106] In some specific embodiments, the data transmission unit may specifically include:
[0107] The first transmission unit is used to transmit each network data packet from the physical network card to the kernel protocol stack based on a hard interrupt.
[0108] In some specific embodiments, the data transmission unit may specifically include:
[0109] The second transmission unit is used to transmit each network data packet from the physical network card to the kernel protocol stack based on a soft interrupt.
[0110] In some specific embodiments, the device further includes:
[0111] The component deployment unit is used to pre-deploy target application components for providing external services on a preset container orchestration platform, so that the client can request the services of the target application components.
[0112] Figure 5 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Specifically, it may include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. The memory 22 stores a computer program, which is loaded and executed by the processor 21 to implement the relevant steps in the port monitoring method performed by the electronic device disclosed in any of the foregoing embodiments.
[0113] In this embodiment, the power supply 23 is used to provide operating voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and external devices, and the communication protocol it follows can be any communication protocol applicable to the technical solution of this application, and is not specifically limited here; the input / output interface 25 is used to acquire external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs, and is not specifically limited here.
[0114] The processor 21 may include one or more processing cores, such as a quad-core processor or an octa-core processor. The processor 21 may be implemented using at least one hardware form selected from DSP (Digital Signal Processing), FPGA (Field-Programmable Gate Array), and PLA (Programmable Logic Array). The processor 21 may also include a main processor and a coprocessor. The main processor, also known as a CPU (Central Processing Unit), is used to process data in the wake-up state; the coprocessor is a low-power processor used to process data in the standby state. In some embodiments, the processor 21 may integrate a GPU (Graphics Processing Unit), which is responsible for rendering and drawing the content to be displayed on the screen. In some embodiments, the processor 21 may also include an AI (Artificial Intelligence) processor, which is used to handle computational operations related to machine learning.
[0115] In addition, the memory 22, as a carrier for resource storage, can be a read-only memory, random access memory, disk or optical disk, etc. The resources stored on it include operating system 221, computer program 222 and data 223, etc., and the storage method can be temporary storage or permanent storage.
[0116] The operating system 221 manages and controls the various hardware devices and computer programs 222 on the electronic device 20 to enable the processor 21 to perform calculations and processing on the massive amounts of data 223 in the memory 22. The operating system 221 can be Windows, Unix, Linux, etc. The computer program 222, in addition to including a computer program capable of performing the port listening method executed by the electronic device 20 as disclosed in any of the foregoing embodiments, may further include computer programs capable of performing other specific tasks. The data 223 may include data received by the electronic device from external devices, as well as data collected by its own input / output interface 25.
[0117] Furthermore, embodiments of this application also disclose a computer-readable storage medium storing a computer program, which, when loaded and executed by a processor, implements the port monitoring method steps disclosed in any of the foregoing embodiments.
[0118] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the apparatus disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple; relevant parts can be referred to in the method section.
[0119] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0120] The steps of the methods or algorithms described in conjunction with the embodiments disclosed herein can be implemented directly by hardware, a software module executed by a processor, or a combination of both. The software module can be located in random access memory (RAM), main memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, removable disk, compact disc read-only memory (CD-ROM), or any other form of storage medium known in the art.
[0121] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0122] The present invention provides a detailed description of a port monitoring method, apparatus, device, and storage medium. Specific examples have been used to illustrate the principles and implementation methods of the present invention. The descriptions of the above embodiments are only for the purpose of helping to understand the method and core ideas of the present invention. At the same time, those skilled in the art will recognize that, based on the ideas of the present invention, there will be changes in the specific implementation methods and application scope. Therefore, the content of this specification should not be construed as a limitation of the present invention.
Claims
1. A port listening method, characterized in that, Applied to servers, including: Obtain the network packets used by the client to request services from the target application components; When each network data packet is passed to the transport layer, the target port information in the network data packet is listened to using a preset socket in the server, and the target distribution program pre-injected in the preset socket is run; wherein, the target distribution program is a distribution program pre-injected into the preset socket using a detector built based on the extended Berkeley packet filter; The target distribution program selects a target port and a target socket that match the target port information from a first memory and a second memory that have been pre-associated, respectively, to transmit the network data packets using the target port and the target socket, and then terminates the kernel's general search process; wherein, the first memory is pre-injected with the ports that the target application component needs to listen to and records the correspondence between the ports and the corresponding socket addresses, and the second memory pre-stores the socket handles of the target application component obtained by the detector; Before obtaining the network data packets used by the client to request services from the target application component, the method further includes: A detector is built based on an extended Berkeley package filter and then loaded into the kernel. The first memory and the second memory are loaded into the kernel, and an association is established between the detector and the second memory; The target distribution program is injected into the preset socket using a detector built based on the extended Berkeley packet filter; The socket handle of the target application component is obtained using a detector built based on the extended Berkeley packet filter, and the socket handle is stored in the second memory based on the association relationship; According to the preset configuration information, a number of ports that the target application component needs to listen to are obtained, and the ports are injected into the first memory using a port injector, and a correspondence between the ports and socket addresses is established in the first memory. The step of using the target distribution program to select a target port and a target socket that match the target port information from a first memory and a second memory with pre-established associations includes: The target dispatch program is used to search for a target port in the first memory that matches the target port information, and the target socket address corresponding to the target port is determined based on the correspondence. Search for the target socket address that matches the target socket address from the socket handles stored in the second memory.
2. The port monitoring method according to claim 1, characterized in that, The method of injecting the target distribution program into the preset socket using a detector built based on an extended Berkeley packet filter includes: Obtain the namespace to which the preset socket belongs; The target distributor is injected into the file handle of the namespace using a detector built based on the extended Berkeley package filter.
3. The port monitoring method according to claim 1, characterized in that, The acquisition of network data packets used by the client to request services from the target application component includes: Use the physical network interface card to obtain the network data packets used by the client to request services from the target application components; Each network data packet is transmitted from the physical network card to the kernel protocol stack based on a preset interrupt method; the kernel protocol stack includes the application layer, the transport layer, the network layer, the data link layer, and the physical layer.
4. The port monitoring method according to claim 3, characterized in that, The transmission of each network data packet from the physical network card to the kernel protocol stack based on a preset interrupt method includes: The network data packets are transmitted from the physical network card to the kernel protocol stack based on hard interrupts; Alternatively, each network data packet can be transmitted from the physical network card to the kernel protocol stack based on a soft interrupt.
5. The port monitoring method according to any one of claims 1 to 4, characterized in that, Also includes: The target application component is pre-deployed on a preset container orchestration platform to provide external services, so that the client can request the services of the target application component.
6. A port monitoring device, characterized in that, Applied to servers, including: The data packet acquisition module is used to acquire the network data packets used by the client to request services from the target application components; The program execution module is used to listen to the target port information in each network data packet when it is passed to the transport layer using a preset socket in the server, and to run a target distribution program pre-injected in the preset socket; wherein, the target distribution program is a distribution program pre-injected into the preset socket using a detector built based on the extended Berkeley packet filter; The matching module is used to select a target port and a target socket that match the target port information from a first memory and a second memory that have been pre-assigned in a pre-established relationship, so as to transmit the network data packet using the target port and the target socket, and then terminate the kernel-general search process; wherein, the first memory is pre-injected with the ports that the target application component needs to listen to and records the correspondence between the ports and the corresponding socket addresses, and the second memory pre-stores the socket handles of the target application component obtained by the detector; Prior to the data packet acquisition module, the device further includes: The first loading unit is used to build a detector based on the extended Berkeley package filter and load the detector into the kernel; The second loading unit is used to load the first memory and the second memory into the kernel, and to establish an association between the detector and the second memory; The program injection unit is used to inject the target distribution program into the preset socket using a detector built based on the extended Berkeley packet filter; The first storage unit is used to obtain the socket handle of the target application component using a detector built based on the extended Berkeley packet filter, and to store the socket handle in the second memory based on the association relationship. The second storage unit is used to obtain a number of ports that the target application component needs to listen to according to preset configuration information, so as to inject the ports into the first memory using a port injector, and establish a correspondence between the ports and socket addresses in the first memory. The matching module specifically includes: The first lookup unit is used to use the target distribution program to find a target port that matches the target port information from the first memory, and to determine the target socket address corresponding to the target port based on the correspondence. The second lookup unit is used to search for a target socket that matches the target socket address from the socket handles stored in the second memory.
7. An electronic device, characterized in that, include: Memory, used to store computer programs; A processor for executing the computer program to implement the steps of the port listening method as described in any one of claims 1 to 5.
8. A computer-readable storage medium, characterized in that, Used to store a computer program; wherein, when the computer program is executed by a processor, it implements the steps of the port listening method as described in any one of claims 1 to 5.