基于包处理的HTTP服务器及其数据处理方法HTTP server based on packet processing and data processing method thereof
技术领域 Technical field
本发明涉及网络技术领域,更具体地说,涉及一种基于包处理的HTTP服务器及其数据处理方法。 The present invention relates to the field of network technologies, and in particular, to an HTTP server based on packet processing and a data processing method thereof.
背景技术Background technique
超文本传输协议(Hypertext Transfer Protocol,简称'HTTP '
)是用于从万维网服务器传输超文本到本地浏览器的传送协议。目前的HTTP服务器都是基于传输控制协议(Transmission Control
Protocol,简称'TCP')之上实现的,如图3所示,该HTTP服务器处理客户端发送来的数据包的具体过程是:客户端发送到HTTP服务器的数据包首先由网卡驱动进行处理,将网络数据帧还原为IP数据包;然后网卡驱动将IP数据包交给内核的协议栈处理,协议栈对数据包进行解析,解析得到TCP数据包时则进行序列号核对、排序等操作,以确保TCP传输的可靠性;协议栈处理好后,应用层的TCP套接字则收到消息,在TCP协议的数据内容之上再进行解析,得到HTTP数据后进入HTTP的相关逻辑处理。
Hypertext Transfer Protocol ('HTTP')
) is a transport protocol for transferring hypertext from a web server to a local browser. The current HTTP server is based on the Transmission Control Protocol (Transmission Control).
Protocol, abbreviated as 'TCP', as shown in FIG. 3, the specific process of the HTTP server processing the data packet sent by the client is: the data packet sent by the client to the HTTP server is first processed by the network card driver. The network data frame is restored to an IP data packet; then the network card driver delivers the IP data packet to the protocol stack of the kernel, and the protocol stack parses the data packet, and when the TCP data packet is parsed, the serial number check and sort operations are performed, Ensure the reliability of TCP transmission; after the protocol stack is processed, the TCP socket of the application layer receives the message and parses it on the data content of the TCP protocol to obtain the HTTP data and enter the relevant logic processing of HTTP.
上述HTTP服务器处理客户端发送来的数据包需经过层层解析,层层拷贝,处理逻辑十分复杂。因而,HTTP服务器支持的连接数非常有限,对于一些小数据量且需要频繁与服务器交互的应用场景,目前的HTTP服务器面对客户端的海量请求时,协议栈会消耗很大的系统资源,因此性能低下。
The above HTTP server processes the data packets sent by the client through layer layer resolution, layer-by-layer copying, and the processing logic is very complicated. Therefore, the number of connections supported by the HTTP server is very limited. For some small data volumes and applications that need to frequently interact with the server, when the current HTTP server faces a large number of requests from the client, the protocol stack consumes a large amount of system resources, so performance low.
发明内容Summary of the invention
基于此,有必要提供一种基于包处理的HTTP服务器,能处理客户端的海量数据请求且处理性能更好。 Based on this, it is necessary to provide a packet processing-based HTTP server that can handle massive data requests from clients and has better processing performance.
此外,还有必要提供一种HTTP服务器数据处理方法,能处理客户端的海量数据请求且处理性能更好。 In addition, it is necessary to provide an HTTP server data processing method that can handle massive data requests from clients and has better processing performance.
本发明提供了一种基于包处理的HTTP服务器,采用原始套接字对通讯端口的数据包进行处理。 The invention provides an HTTP server based on packet processing, which uses raw sockets to process data packets of a communication port.
优选地,HTTP服务器接收客户端发送的数据,经网卡驱动处理后以内存映射的方式将数据拷贝到应用层的内存缓冲区中。Preferably, the HTTP server receives the data sent by the client, and copies the data to the memory buffer of the application layer in a memory mapping manner after being processed by the network card driver.
进一步优选地,网卡驱动可用于将接收到的网络数据帧转换为IP数据包。 Further preferably, the network card driver can be used to convert the received network data frame into an IP data packet.
优选地,
HTTP服务器在应用层构造IP数据包,并将IP数据包发送给IP协议栈,由IP协议栈将IP数据包发送给网卡驱动,网卡驱动对IP数据包进行处理后发送给客户端。 Preferably,
The HTTP server constructs an IP data packet at the application layer, and sends the IP data packet to the IP protocol stack, and the IP data packet is sent to the network card driver by the IP protocol stack, and the network card driver processes the IP data packet and sends it to the client.
进一步优选地,网卡驱动可用于将IP数据包转换为网络数据帧。 Further preferably, the network card driver can be used to convert IP data packets into network data frames.
本发明还提供了一种HTTP服务器数据处理方法,包括:所述HTTP服务器采用原始套接字对通讯端口的数据包进行处理。
The invention also provides an HTTP server data processing method, comprising: the HTTP server processing the data packet of the communication port by using the original socket.
优选地,HTTP服务器采用原始套接字对通讯端口的数据包进行处理的步骤可包括:
HTTP服务器接收客户端发送的数据,经网卡驱动处理后以内存映射的方式将数据拷贝到应用层的内存缓冲区中。 Preferably, the step of the HTTP server processing the data packet of the communication port by using the original socket may include:
The HTTP server receives the data sent by the client, and copies the data to the memory buffer of the application layer in a memory mapping manner after being processed by the network card driver.
进一步优选地,网卡驱动处理的步骤具体可以是:网卡驱动将接收到的网络数据帧转换为IP数据包。 Further preferably, the step of the network card driving process may specifically be: the network card driver converts the received network data frame into an IP data packet.
优选地,HTTP服务器采用原始套接字对通讯端口的数据包进行处理的步骤还可包括:
HTTP服务器在应用层构造IP数据包,并将IP数据包发送给IP协议栈,由IP协议栈将IP数据包发送给网卡驱动,网卡驱动对IP数据包进行处理后发送给客户端。 Preferably, the step of processing, by the HTTP server, the data packet of the communication port by using the original socket may further include:
The HTTP server constructs an IP data packet at the application layer, and sends the IP data packet to the IP protocol stack, and the IP data packet is sent to the network card driver by the IP protocol stack, and the network card driver processes the IP data packet and sends it to the client.
进一步优选地,网卡驱动对IP数据包进行处理的步骤具体可是:网卡驱动将IP数据包转换为网络数据帧。 Further preferably, the step of processing the IP data packet by the network card driver may specifically be: the network card driver converts the IP data packet into a network data frame.
一种基于包处理的HTTP服务器,包括:网卡驱动,接收数据包;内存缓冲区,以内存映射的方式拷贝经所述网卡驱动处理后的数据包;以及HTTP服务器,采用原始套接字对数据包进行处理。
An HTTP server based on packet processing, comprising: a network card driver, receiving a data packet; a memory buffer copying the data packet processed by the network card driver in a memory mapping manner; and an HTTP server, using the original socket pair data The package is processed.
优选地,所述网卡驱动将接收到的网络数据帧转换为IP数据包。 Preferably, the network card driver converts the received network data frame into an IP data packet.
优选地,所述的基于包处理的HTTP服务器进一步包括IP协议栈;所述HTTP服务器在应用层构造IP数据包,并将所述IP数据包发送给IP协议栈,由IP协议栈将IP数据包发送给网卡驱动,所述网卡驱动对IP数据包进行处理后发送给客户端。
Preferably, the packet processing-based HTTP server further includes an IP protocol stack; the HTTP server constructs an IP data packet at an application layer, and sends the IP data packet to an IP protocol stack, and the IP protocol stack transmits IP data. The packet is sent to the network card driver, and the network card driver processes the IP data packet and sends it to the client.
进一步优选地,所述网卡驱动用于将IP数据包转换为网络数据帧。 Further preferably, the network card driver is configured to convert IP data packets into network data frames.
上述基于包处理的HTTP服务器及其数据处理方法,通过采用原始套接字对通讯端口的数据包进行处理,在HTTP服务器接收客户端发送的数据时,无需将数据交给协议栈,而是直接以内存映射的方式将数据拷贝到应用层的内存缓冲区中,HTTP服务器则可直接使用这些数据,不需要再经过套接字的其它处理,减少了很大部分的系统资源消耗。由于是在IP协议的层面上实现HTTP服务器的数据处理,数据包未进入TCP协议栈,因此提高了HTTP服务器的响应速度,使HTTP服务器能处理客户端的海量数据请求;另一方面,HTTP服务器返回数据给客户端时,直接由应用层构造好IP数据包,将IP数据包发送给IP协议栈,再由IP协议栈发送给网卡驱动,由于原始套接字的数据包发送的性能远高于TCP套接字的数据发送,因此也提高了性能。
The above-mentioned packet processing-based HTTP server and its data processing method process the data packet of the communication port by using the original socket, and when the HTTP server receives the data sent by the client, it is not necessary to hand the data to the protocol stack, but directly The data is copied to the memory buffer of the application layer in a memory-mapped manner, and the HTTP server can directly use the data, and does not need to go through other processing of the socket, thereby reducing a large part of system resource consumption. Since the data processing of the HTTP server is implemented on the IP protocol level, the data packet does not enter the TCP protocol stack, thereby improving the response speed of the HTTP server, enabling the HTTP server to process the massive data request of the client; on the other hand, the HTTP server returns When the data is sent to the client, the IP data packet is directly constructed by the application layer, and the IP data packet is sent to the IP protocol stack, and then sent by the IP protocol stack to the network card driver, because the performance of the data transmission of the original socket is much higher than that. TCP sockets are sent for data, which also improves performance.
附图说明DRAWINGS
图1是一个实施例中客户端与HTTP服务器进行交互的示意图; 1 is a schematic diagram of a client interacting with an HTTP server in an embodiment;
图2是一个实施例中HTTP服务器数据处理方法的原理图; 2 is a schematic diagram of an HTTP server data processing method in an embodiment;
图3是现有技术中HTTP服务器数据处理方法的原理图。 3 is a schematic diagram of a prior art HTTP server data processing method.
具体实施方式detailed description
由于HTTP协议的通讯过程一般都是短连接,通讯模式上是请求-响应模式。参照图1,客户端与HTTP服务器之间的一次交互过程如下:
Since the communication process of the HTTP protocol is generally a short connection, the communication mode is a request-response mode. Referring to Figure 1, an interaction process between the client and the HTTP server is as follows:
(1)客户端发送SYN包给HTTP服务器; (1) The client sends a SYN packet to the HTTP server;
(2)HTTP服务器发送SYN+ACK包给客户端; (2) The HTTP server sends a SYN+ACK packet to the client;
(3)客户端发送ACK包给HTTP服务器; (3) The client sends an ACK packet to the HTTP server;
(4)客户端发送内容为GET的包给HTTP服务器; (4) The client sends a packet with the content GET to the HTTP server;
(5)HTTP服务器返回内容为200 OK的包给客户端; (5) The HTTP server returns a packet with a content of 200 OK to the client;
(6)数据发送完成后关闭连接。 (6) Close the connection after the data transmission is completed.
其中,SYN包是握手包,ACK是应答包,FIN是结束包,上述步骤(1)至(3)是典型的TCP三次握手过程;而GET包是HTTP协议的请求包,200
OK是HTTP服务器返回给客户端的内容包。由上述内容可知,客户端与HTTP服务器的一次通讯包括:客户端发送给HTTP服务器的三次数据包(SYN包、ACK包和GET包)和HTTP服务器响应给客户端的两次数据包(ACK+SYN的握手应答包和ACK+FIN且内容为200
OK的与业务相关的应答包)。
The SYN packet is a handshake packet, the ACK is a response packet, and the FIN is an end packet. The above steps (1) to (3) are typical TCP three-way handshake procedures; and the GET packet is a request packet of the HTTP protocol, 200.
OK is the content package returned by the HTTP server to the client. It can be seen from the above that the first communication between the client and the HTTP server includes three data packets (SYN packets, ACK packets, and GET packets) sent by the client to the HTTP server and two data packets (ACK+SYN) that the HTTP server responds to the client. Handshake reply packet and ACK+FIN with content of 200
OK's business-related response package).
在一个实施方式中,本发明提供的基于包处理的HTTP服务器采用原始套接字对通讯端口的数据包进行处理。这里的通讯端口为默认的80端口,原始套接字是与TCP套接字不同的一种网络通信方式,其能够采集链路层的数据包,由于链路层之上是IP协议层,IP协议层之上是TCP协议层,而原始套接字是最底层的应用程序编程接口(Application
Programming Interface,简称'API'),因此能够采集所有层的数据包。
In one embodiment, the packet processing-based HTTP server provided by the present invention processes the data packets of the communication port using the original socket. The communication port here is the default port 80. The original socket is a different network communication method than the TCP socket. It can collect the data packets of the link layer. Because the link layer is the IP protocol layer, IP. Above the protocol layer is the TCP protocol layer, and the original socket is the lowest level application programming interface (Application
The Programming Interface (referred to as 'API') is therefore able to collect packets from all layers.
参见图2,本发明的基于包处理的HTTP服务器包括:网卡驱动、IP协议栈、内存缓冲区和HTTP服务器;
Referring to FIG. 2, the packet processing-based HTTP server of the present invention includes: a network card driver, an IP protocol stack, a memory buffer, and an HTTP server;
网卡驱动接收数据包,并将接收到的网络数据帧转换为IP数据包以及将IP数据包转换为网络数据帧; The network card driver receives the data packet, and converts the received network data frame into an IP data packet and converts the IP data packet into a network data frame;
内存缓冲区以内存映射的方式拷贝经网卡驱动处理后的数据包; The memory buffer copies the data packet processed by the network card driver in a memory mapping manner;
HTTP
服务器采用原始套接字对数据包进行处理,在应用层构造IP数据包,并将IP数据包发送给IP协议栈,由IP协议栈将IP数据包发送给网卡驱动,网卡驱动对IP数据包进行处理后发送给客户端。 HTTP
The server uses the original socket to process the data packet, constructs an IP data packet in the application layer, and sends the IP data packet to the IP protocol stack, and the IP protocol stack sends the IP data packet to the network card driver, and the network card driver drives the IP data packet. After processing, send it to the client.
在一个实施方式中,如图2所示,HTTP服务器采用原始套接字对通讯端口的数据包进行处理的过程包括接收客户端发送的数据的处理过程及返回数据至客户端的处理过程。其中,HTTP服务器接收客户端发送的数据的过程如下:(1)客户端发送数据至网卡驱动。网卡驱动接收的是客户端通过网络传送过来的网络数据帧;(2)网卡驱动将接收到的网络数据帧转换为IP数据包;(3)网卡驱动将处理得到的IP数据包以内存映射的方式将数据拷贝到应用层的内存缓冲区中。IP数据包复制到内存中后,HTTP服务器则可以直接使用这些数据包,而无需再经过套接字的其它处理过程。
In one embodiment, as shown in FIG. 2, the process of processing the data packet of the communication port by using the original socket by the HTTP server includes a process of receiving data sent by the client and a process of returning data to the client. The process of the HTTP server receiving the data sent by the client is as follows: (1) The client sends data to the network card driver. The network card driver receives the network data frame transmitted by the client through the network; (2) the network card driver converts the received network data frame into an IP data packet; and (3) the network card driver processes the obtained IP data packet into a memory mapped The way to copy the data to the memory buffer of the application layer. After IP packets are copied into memory, the HTTP server can use these packets directly without having to go through other socket processing.
如图2所示,HTTP服务器响应客户端的过程如下:(1)HTTP服务器在应用层构造IP数据包,并将IP数据包发送给IP协议栈;(2)IP协议栈将IP数据包发送给网卡驱动;(3)网卡驱动对IP数据包进行处理。具体地,网卡驱动是将IP数据包转换为网络数据帧。(4)网卡驱动将网络数据帧通过网络发送到客户端。
As shown in Figure 2, the HTTP server responds to the client as follows: (1) The HTTP server constructs an IP packet at the application layer and sends the IP packet to the IP protocol stack; (2) The IP protocol stack sends the IP packet to NIC driver; (3) NIC driver to process IP packets. Specifically, the network card driver converts IP data packets into network data frames. (4) The network card driver sends the network data frame to the client through the network.
上述基于包处理的HTTP服务器及其数据处理方法,数据在处理的过程中只需要复制一次,而不需要层层复制,因此数据处理的流程减少了,对于应用程序而言,接收数据的流程也更简单,其能支持的连接数将更高。由于HTTP服务器的数据处理过程是在IP协议的层面上实现,数据包未进入TCP协议栈,因此HTTP服务器的响应速度更快,且对服务器的消耗更小。另外,上述基于包处理的HTTP服务器由于绕过了TCP协议栈,因此服务器端并不需要保存客户端和服务器的连接状态,当客户端发起针对TCP协议栈的攻击时,对该基于包处理的HTTP服务器并不适用,因此能有效抵制恶意的SYN
FLOOD或慢连接。
In the above-mentioned packet processing-based HTTP server and its data processing method, data only needs to be copied once in the process of processing, and no layer-level copying is required, so the flow of data processing is reduced, and for the application, the process of receiving data is also Simpler, the number of connections it can support will be higher. Since the data processing process of the HTTP server is implemented at the level of the IP protocol, the data packet does not enter the TCP protocol stack, so the response speed of the HTTP server is faster and the consumption of the server is smaller. In addition, since the above-mentioned packet processing-based HTTP server bypasses the TCP protocol stack, the server side does not need to save the connection state between the client and the server. When the client initiates an attack against the TCP protocol stack, the packet processing is performed. HTTP server is not applicable, so it can effectively resist malicious SYN
FLOOD or slow connection.
上述基于包处理的HTTP服务器及其数据处理方法,由于可支持的连接数很高,可广泛应用于具有海量请求数且每次请求和响应的数据传输量非常小的HTTP应用。例如:统计服务器(如网络广告上的计数器)、聊天服务器(如基于HTTP的双人聊天或者聊天室)、验证码服务器(如生成复杂的验证码图片)、头像图片服务器(如处理GZIP压缩后的小于1460字节的图片)、以及其它请求和响应内容小于1460字节的HTTP应用。
The above-described packet processing-based HTTP server and its data processing method are widely applicable to HTTP applications having a large number of requests and a very small amount of data transmission per request and response because of the high number of connections that can be supported. For example: statistics server (such as counters on online advertising), chat server (such as HTTP-based two-person chat or chat room), verification code server (such as generating complex captcha pictures), avatar image server (such as processing GZIP compressed Images smaller than 1460 bytes), and other HTTP applications with less than 1460 bytes of request and response content.
以上所述仅为本发明的较佳实施例而已,并不用以限制本发明,凡在本发明的精神和原则之内所作的任何修改、等同替换和改进等,均应包含在本发明的保护范围之内。
The above is only the preferred embodiment of the present invention, and is not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection of the present invention. Within the scope.