Method and device for capturing network card data packet and electronic equipment

文档序号:1589720 发布日期:2020-01-03 浏览:17次 中文

阅读说明:本技术 抓取网卡数据包的方法、装置及电子设备 (Method and device for capturing network card data packet and electronic equipment ) 是由 陈闯 潘飚 王鹤 王能洁 于 2019-10-16 设计创作,主要内容包括:本发明提供了一种抓取网卡数据包的方法、装置及电子设备,涉及计算机技术领域,该方法包括:设置网卡的网卡队列按照指定顺序接收数据包;其中,网卡的网卡队列为至少一个;查找网卡队列中的目标网卡队列;获取目标网卡队列接收的数据包,并通过执行预设函数对获取到的数据包进行内存拷贝,将内存拷贝后的数据包作为抓取到的网卡数据包;其中,预设函数是用于修改数据包引用计数的函数。本发明有效提升了抓取网卡数据包的效率。(The invention provides a method, a device and electronic equipment for capturing a network card data packet, which relate to the technical field of computers, and the method comprises the following steps: setting a network card queue of a network card to receive data packets according to a specified sequence; the network card queue of the network card is at least one; searching a target network card queue in the network card queue; acquiring a data packet received by a target network card queue, performing memory copy on the acquired data packet by executing a preset function, and taking the data packet after the memory copy as a captured network card data packet; wherein the preset function is a function for modifying the packet reference count. The invention effectively improves the efficiency of capturing the network card data packet.)

1. A method for capturing a network card data packet is characterized by comprising the following steps:

setting a network card queue of the network card to receive data packets according to a specified sequence; the network card queue of the network card is at least one;

searching a target network card queue in the network card queue;

acquiring a data packet received by the target network card queue, performing memory copy on the acquired data packet by executing a preset function, and taking the data packet after memory copy as a captured network card data packet; wherein the preset function is a function for modifying a packet reference count.

2. The method according to claim 1, wherein the step of performing the memory copy on the acquired data packet by executing a preset function includes:

acquiring a first reference count of the data packet, and performing value-added operation on the first reference count by executing the preset function to obtain a second reference count;

and performing memory copy on the data packet according to the second reference count.

3. The method according to claim 1, wherein the step of setting the network card queue of the network card to receive the data packets according to a specified sequence comprises:

acquiring queue configuration information of a network card queue of the network card; the queue configuration information comprises the number of queues and the length of the queues;

and setting a network card queue of the network card to receive data packets according to the acquired queue configuration information and the specified sequence.

4. The method according to claim 1, wherein the step of searching the target network card queue in the network card queue comprises:

searching a network card queue of the received data packet in the network card queue;

and determining the network card queue of the received data packet as a target network card queue.

5. The method of claim 1, further comprising: and writing the captured network card data packet into a specified file.

6. The method according to any of claims 1 to 5, wherein the method is applied to a dpdk-pdump bale plucker.

7. The utility model provides a snatch device of network card data package which characterized in that includes:

the network card setting module is used for setting a network card queue of the network card to receive data packets according to a specified sequence; the network card queue of the network card is at least one;

the queue searching module is used for searching a target network card queue in the network card queue;

the packet capturing module is used for acquiring the data packet received by the target network card queue, performing memory copy on the acquired data packet by executing a preset function, and taking the data packet after the memory copy as a captured network card data packet; wherein the preset function is a function for modifying a packet reference count.

8. The apparatus of claim 7, wherein the bale plucking module is configured to:

acquiring a first reference count of the data packet, and adding a value to the first reference count by executing the preset function to obtain a second reference count;

and performing memory copy on the data packet according to the second reference count.

9. An electronic device comprising a processor and a memory, the memory storing computer-executable instructions executable by the processor, the processor executing the computer-executable instructions to implement the steps of the method of grabbing a network card packet of any one of claims 1 to 6.

10. A computer-readable storage medium, on which a computer program is stored, which, when being executed by a processor, performs the steps of the method for capturing network card data packets according to any one of claims 1 to 6.

Technical Field

The invention relates to the technical field of computers, in particular to a method and a device for capturing a network card data packet and electronic equipment.

Background

With the development of the internet, the data packet flow in the network is larger and larger, and the improvement of the capturing efficiency of capturing the network card data packet is of great importance. At present, when a network card data packet is captured, a memory copy is usually performed on the data packet first, the copied result is written into a pcap file, and the network card data packet after the memory copy is used as the captured network card data packet. However, the memory of the data packet is usually large, and the time consumed for copying the memory of the data packet is long, which results in low efficiency of capturing the network card data packet. Aiming at the problem of capturing the network card data packet in practical application, an effective solution is not provided at present.

Disclosure of Invention

The invention aims to provide a method, a device and electronic equipment for capturing a network card data packet, so as to relieve the technical problem that the efficiency of capturing the data packet is lower due to longer time consumption of memory copy when the network card data packet is captured in the prior art, and effectively improve the efficiency of capturing the network card data packet.

In a first aspect, an embodiment of the present invention provides a method for capturing a network card data packet, including: setting a network card queue of a network card to receive data packets according to a specified sequence; the network card queue of the network card is at least one; searching a target network card queue in the network card queue; acquiring a data packet received by a target network card queue, performing memory copy on the acquired data packet by executing a preset function, and taking the data packet after the memory copy as a captured network card data packet; wherein the preset function is a function for modifying the packet reference count.

Further, the step of performing memory copy on the obtained data packet by executing the preset function includes: acquiring a first reference count of the data packet, and performing value-added operation on the first reference count by executing a preset function to obtain a second reference count; and performing memory copy on the data packet according to the second reference count.

Further, the step of receiving the data packets by the network card queue with the network card according to the designated sequence includes: acquiring queue configuration information of a network card queue of a network card; the queue configuration information comprises the number of queues and the length of the queues; and setting a network card queue of the network card to receive the data packets according to the specified sequence according to the acquired queue configuration information.

Further, the step of searching for the target network card queue in the network card queue includes: searching a network card queue receiving the data packet in the network card queue; and determining the network card queue of the received data packet as a target network card queue.

Further, the method further comprises: and writing the captured network card data packet into a specified file.

In a second aspect, an embodiment of the present invention provides an apparatus for capturing a network card data packet, including: the network card setting module is used for setting a network card queue of the network card to receive data packets according to a specified sequence; the network card queue of the network card is at least one; the queue searching module is used for searching a target network card queue in the network card queue; the packet capturing module is used for acquiring the data packets received by the target network card queue, performing memory copy on the acquired data packets by executing a preset function, and taking the data packets after the memory copy as captured network card data packets; wherein the preset function is a function for modifying the packet reference count.

Further, the packet capturing module is configured to: acquiring a first reference count of the data packet, and adding a value of the first reference count by executing a preset function to obtain a second reference count; and performing memory copy on the data packet according to the second reference count.

In a third aspect, an embodiment of the present invention provides an electronic device, which includes a processor and a memory, where the memory stores computer-executable instructions that can be executed by the processor, and the processor executes the computer-executable instructions to implement the steps of the method for capturing the network card data packet in any one of the foregoing implementation manners.

In a fourth aspect, an embodiment of the present invention provides a computer-readable storage medium, where a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, the computer program performs the steps of the method for capturing the network card data packet according to any one of the foregoing embodiments.

The method, the device and the electronic equipment for capturing the network card data packet can set a network card queue of a network card to receive the data packet according to a designated sequence, search a target network card queue in the network card queue, further acquire the data packet received by the target network card queue, perform memory copy on the acquired data packet by executing a preset function (a function for modifying reference counting of the data packet), and take the data packet after the memory copy as the captured network card data packet. According to the mode, the preset function for modifying the reference count is executed, the data packet can be subjected to memory copy when the data packet is captured, the time for memory copy is shortened, and therefore the efficiency for capturing the data packet is effectively improved.

Additional features and advantages of the invention will be set forth in the description which follows, and in part will be obvious from the description, or may be learned by practice of the invention. The objectives and other advantages of the invention will be realized and attained by the structure particularly pointed out in the written description and claims hereof as well as the appended drawings.

In order to make the aforementioned and other objects, features and advantages of the present invention comprehensible, preferred embodiments accompanied with figures are described in detail below.

Drawings

In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, and it is obvious that the drawings in the following description are some embodiments of the present invention, and other drawings can be obtained by those skilled in the art without creative efforts.

Fig. 1 is a schematic flowchart of a method for capturing a network card data packet according to an embodiment of the present invention;

fig. 2 is a schematic flowchart of another method for capturing a network card data packet according to an embodiment of the present invention;

fig. 3 is a schematic structural diagram of a device for capturing a network card data packet according to an embodiment of the present invention;

fig. 4 is a schematic structural diagram of an electronic device according to an embodiment of the present invention.

Detailed Description

The technical solutions of the present invention will be described clearly and completely with reference to the following embodiments, and it should be understood that the described embodiments are some, but not all, embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.

In view of the fact that in the prior art, when capturing a network card data packet, the time consumption of memory copy is long, so that the efficiency of capturing the data packet is low. Based on this, the embodiment of the invention provides a method and a device for capturing a network card data packet and an electronic device, which can effectively improve the efficiency of capturing the data packet.

To facilitate understanding of the present embodiment, first, a method for capturing a network card data packet disclosed in the present embodiment is described in detail, referring to a flowchart of a method for capturing a network card data packet shown in fig. 1, where the method mainly includes the following steps S102 to S106:

step S102: and the network card queue of the network card is set to receive the data packets according to the designated sequence.

In one embodiment, the queues of the network cards are configured to receive data packets, and since the network card multi-queue is configured to enable high-speed data packet capture, at least one network card queue of each network card is configured, and the configured network card queues receive data packets according to a specified sequence, for example, the network card queues can receive data packets according to a circular sequence.

Step S104: searching a target network card queue in the network card queue;

after receiving the data packets according to the above-mentioned designated sequence, the network card data packets need to be captured by searching the network card queue (also called target network card queue) for receiving the data packets in the plurality of network card queues.

Step S106: and acquiring a data packet received by the target network card queue, performing memory copy on the acquired data packet by executing a preset function, and taking the data packet after the memory copy as a captured network card data packet.

In an embodiment, memory copy of an acquired network card data packet is realized by executing a preset function, the preset function is a function for modifying a reference count of the data packet, and memory copy of the data packet is realized by modifying the reference count of the data packet.

The method for capturing the network card data packet provided by the embodiment of the invention can set the network card queue of the network card to receive the data packet according to the designated sequence, search the target network card queue in the network card queue, further obtain the data packet received by the target network card queue, perform memory copy on the obtained data packet by executing the preset function (the function for modifying the reference count of the data packet), and use the data packet after the memory copy as the captured network card data packet. According to the mode, the preset function for modifying the reference count is executed, the data packet can be subjected to memory copy when the data packet is captured, the time for memory copy is shortened, and therefore the efficiency for capturing the data packet is effectively improved.

To facilitate understanding of step S102 in the foregoing embodiment, a specific implementation manner for setting a network card queue is provided in the foregoing embodiment of the present invention, where the step S102 mainly includes the following steps 1 to 2:

step 1, obtaining queue configuration information of a network card queue of a network card, wherein the queue configuration information comprises the number of the queues and the length of the queues.

And 2, setting a network card queue of the network card to receive the data packets according to the acquired queue configuration information and the specified sequence. The network card queues are configured through the queue configuration information, that is, the number and the length of the network card use queues are configured, and the configured network cards are started, so that the configured network card queues can receive data packets on the network according to a specified sequence, for example, the data packets can be received according to a cycle sequence.

In an embodiment, in order to facilitate understanding of step S104, an embodiment of the present invention provides a specific method for searching a target network card queue in a network card queue, where the method includes the following steps a and b:

step a, searching a network card queue receiving the data packet in the network card queue.

And b, determining the network card queue of the received data packet as a target network card queue.

Since the number of the network card queues is configured in the step 1, and the number of the network card queues is multiple, the network card queues which currently receive the data packets are determined as target network card queues by searching in the network card queues, so that the data packets are conveniently captured from the network card queues.

In an embodiment, to facilitate understanding of the step S106, an embodiment of the present invention provides a specific method for performing memory copy on an obtained data packet by executing a preset function, where reference is made to the following steps (1) to (2):

and (1) acquiring a first reference count of the data packet, and performing value-added operation on the first reference count by executing a preset function to obtain a second reference count. The information of the data packet may include information such as a memory virtual address, a physical address, a length, a hash value, a memory pool to which the data packet belongs, a reference count, and the like of the data packet, and after the network card data packet of the target queue is obtained, a reference count (i.e., a first reference count) of the data packet is obtained, and a value is added to the reference count of the data packet by executing a preset function, such as adding one to the reference count to obtain a new reference count (i.e., a second reference count). The method is characterized in that atomic operation is adopted for modifying the reference count, the atomic operation refers to operation which cannot be interrupted by a thread scheduling mechanism, once the operation is started, the operation is operated till the operation is ended, the atomic operation is used for ensuring that the reference count is only modified by the current thread, and other threads cannot modify the value of the reference count of the current data packet at the same time.

And (2) performing memory copy on the data packet according to the second reference count. The new reference count is obtained through the method, the memory copy of the data packet is realized, and the data packet subjected to the memory copy is used as the captured network card data packet.

In one embodiment, the captured network card data packet is written into a designated file, such as a pcap file.

For convenience of understanding, taking an example that a network card Data packet is captured by a DPDK-pdump packet capturing tool in a DPDK (Data Plane Development Kit ), a specific method for capturing a network card Data packet is described in detail, referring to a flow diagram of another method for capturing a network card Data packet shown in fig. 2, the method mainly includes the following steps S202 to S206:

step S202: and setting a large-page memory.

Firstly, a large-page memory is required to be arranged and used for storing network card information and information of a data packet received by a DPDK main process, compiling a DPDK library and binding the DPDK network card.

Step S204: the DPDK acquires the network card data packet.

In a specific implementation manner, a network card data packet is acquired through a DPDK main process, and when the network card data packet is acquired, a large-page memory needs to be initialized, so as to provide an address storage space for creating a memory pool when the network card data packet is captured. And setting a network card queue of the DPDK network card, and configuring the queue number and length of the network card. Starting the network card, starting the network card to receive the data packet, and circularly acquiring the data packet received by the network card through the set network card queue. In addition, initializing and communicating with the DPDK-pdump through a local socket file, and transmitting a network card data packet acquired by the DPDK to a DPDK-pdump tool.

Step S206: and the DPDK-pdump tool captures the network card data packet.

When a DPDK-pdump tool captures a network card data packet, the specific capturing steps include the following steps A to C:

step A, initializing a large-page memory, in one implementation mode, when a network card data packet is captured, acquiring the large-page memory created by a DPDK main process when the received data packet is the initialized large-page memory by initializing the large-page memory, and using a memory space produced by initializing the large-page memory, so that the address spaces of the data packets received by the network card can be ensured to be the same, and data information of the network card data packet is read in a packet capturing process.

B, searching a target network card queue;

step C, setting a callback function of the target network card queue during packet receiving, capturing a network card data packet, and capturing the data packet by executing the callback function, wherein the operation during execution of the callback function is as follows: and adding one to the reference count of the data packet, writing the data packet into the pcap file, and then subtracting one from the reference count of the data packet. And performing memory copy on the data packet by modifying the reference count, and taking the data packet copied by the memory as a captured data packet.

In practical application, after the network card data packet is captured, the captured data packet is transmitted to an upper application program for processing.

Based on the foregoing method for capturing the network card data packet, an embodiment of the present invention further provides a device for capturing the network card data packet corresponding to the foregoing method for capturing the network card data packet, referring to a schematic structural diagram of the device for capturing the network card data packet shown in fig. 3, where the device mainly includes the following modules:

a network card setting module 302, configured to set a network card queue of a network card to receive data packets according to a specified sequence; the network card queue of the network card is at least one;

the queue searching module 304 is configured to search a target network card queue in the network card queue;

the packet capturing module 306 is configured to acquire a data packet received by the target network card queue, perform memory copy on the acquired data packet by executing a preset function, and use the data packet after memory copy as a captured network card data packet; wherein the preset function is a function for modifying the packet reference count.

The device for capturing the network card data packet provided by the embodiment of the invention can set the network card queue of the network card to receive the data packet according to the designated sequence, search the target network card queue in the network card queue, further obtain the data packet received by the target network card queue, perform memory copy on the obtained data packet by executing the preset function (the function for modifying the reference count of the data packet), and use the data packet after the memory copy as the captured network card data packet. According to the mode, the preset function for modifying the reference count is executed, the data packet can be subjected to memory copy when the data packet is captured, the time for memory copy is shortened, and therefore the efficiency for capturing the data packet is effectively improved.

In an implementation manner, the packet capturing module 306 is further configured to obtain a first reference count of the data packet, and perform value addition on the first reference count by executing a preset function to obtain a second reference count; and performing memory copy on the data packet according to the second reference count.

In an embodiment, the network card setting module 302 is further configured to obtain queue configuration information of a network card queue of each network card; the queue configuration information comprises the number of queues and the length of the queues; and setting the network card queue of each network card to receive the data packets according to the specified sequence according to the acquired queue configuration information.

In an embodiment, the queue searching module 304 is further configured to search a network card queue of the network card queue, where the network card queue receives the data packet; and determining the network card queue of the received data packet as a target network card queue.

In one embodiment, the above apparatus further comprises: and the file writing module is used for writing the captured network card data packet into the specified file.

The device provided by the embodiment of the present invention has the same implementation principle and technical effect as the method embodiments, and for the sake of brief description, reference may be made to the corresponding contents in the method embodiments without reference to the device embodiments.

The embodiment of the invention provides electronic equipment, which particularly comprises a processor and a storage device; the storage means has stored thereon a computer program which, when executed by the processor, performs the method of any of the above described embodiments.

Fig. 4 is a schematic structural diagram of an electronic device 100 according to an embodiment of the present invention, where the electronic device 100 includes: a processor 40, a memory 41, a bus 42 and a communication interface 43, wherein the processor 40, the communication interface 43 and the memory 41 are connected through the bus 42; the processor 40 is arranged to execute executable modules, such as computer programs, stored in the memory 41.

The memory 41 may include a high-speed Random Access Memory (RAM) and may also include a non-volatile memory (non-volatile memory), such as at least one disk memory. The communication connection between the network element of the system and at least one other network element is realized through at least one communication interface 43 (which may be wired or wireless), and the internet, a wide area network, a local network, a metropolitan area network, etc. may be used.

The bus 42 may be an ISA bus, PCI bus, EISA bus, or the like. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one double-headed arrow is shown in FIG. 4, but that does not indicate only one bus or one type of bus.

The memory 41 is used for storing a program, the processor 40 executes the program after receiving an execution instruction, and the method executed by the apparatus defined by the flow process disclosed in any of the foregoing embodiments of the present invention may be applied to the processor 40, or implemented by the processor 40.

The processor 40 may be an integrated circuit chip having signal processing capabilities. In implementation, the steps of the above method may be performed by integrated logic circuits of hardware or instructions in the form of software in the processor 40. The Processor 40 may be a general-purpose Processor, and includes a Central Processing Unit (CPU), a Network Processor (NP), and the like; the device can also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field-Programmable Gate Array (FPGA) or other Programmable logic device, a discrete Gate or transistor logic device, or a discrete hardware component. The various methods, steps and logic blocks disclosed in the embodiments of the present invention may be implemented or performed. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like. The steps of the method disclosed in connection with the embodiments of the present invention may be directly implemented by a hardware decoding processor, or implemented by a combination of hardware and software modules in the decoding processor. The software module may be located in ram, flash memory, rom, prom, or eprom, registers, etc. storage media as is well known in the art. The storage medium is located in a memory 41, and the processor 40 reads the information in the memory 41 and completes the steps of the method in combination with the hardware thereof.

The method, the apparatus, and the computer program product for capturing the network card data packet provided in the embodiments of the present invention include a computer-readable storage medium storing a nonvolatile program code executable by a processor, where a computer program is stored on the computer-readable storage medium, and when the computer program is executed by the processor, the method described in the foregoing method embodiments is executed.

It can be clearly understood by those skilled in the art that, for convenience and brevity of description, the specific working process of the system described above may refer to the corresponding process in the foregoing embodiments, and is not described herein again.

The computer program product of the readable storage medium provided in the embodiment of the present invention includes a computer readable storage medium storing a program code, where instructions included in the program code may be used to execute the method described in the foregoing method embodiment, and specific implementation may refer to the method embodiment, which is not described herein again.

The functions, if implemented in the form of software functional units and sold or used as a stand-alone product, may be stored in a computer readable storage medium. Based on such understanding, the technical solution of the present invention may be embodied in the form of a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, an electronic device, or a network device) to perform all or part of the steps of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk, and other various media capable of storing program codes.

Finally, it should be noted that: the above embodiments are only used to illustrate the technical solution of the present invention, and not to limit the same; while the invention has been described in detail and with reference to the foregoing embodiments, it will be understood by those skilled in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some or all of the technical features may be equivalently replaced; and the modifications or the substitutions do not make the essence of the corresponding technical solutions depart from the scope of the technical solutions of the embodiments of the present invention.

11页详细技术资料下载
上一篇:一种医用注射器针头装配设备
下一篇:一种CIFS网络带宽的优化方法、装置和介质

网友询问留言

已有0条留言

还没有人留言评论。精彩留言会获得点赞!

精彩留言,会给你点赞!