Regular expression pattern matching method and system based on p4 programmable switch

By transforming the set of regular expressions into an empty-edge-free NFA state machine and performing pattern matching on a P4 switch, the problems of empty-edge space occupation and low memory bandwidth in traditional engines are solved, resulting in faster matching speed and higher success rate.

CN118228064BActive Publication Date: 2026-11-24GUANGZHOU UNIVERSITY
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410406735.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-04-03
Publication Date
2026-11-24
Estimated Expiration
2044-04-03

AI Technical Summary

Technical Problem

Traditional regular expression engines suffer from issues such as empty borders occupying space and low memory bandwidth during the matching process, resulting in slow matching speed and high cost.

Method used

The set of regular expressions is transformed into an automaton with empty edges, and then into an NFA state machine without empty edges through a preset empty edge removal algorithm. The edges are then converted into a rule table and sent to the P4 switch for pattern matching.

Benefits of technology

It improves the speed and success rate of regular expression matching, reduces space usage, and enhances the ability to handle high-bandwidth network traffic.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118228064B_ABST
    Figure CN118228064B_ABST
Patent Text Reader

Abstract

This invention discloses a regular expression pattern matching method and system based on a P4 programmable switch, belonging to the application field of programmable switches. The method includes: the control layer acquiring a set of regular expressions; converting the set of regular expressions into an automaton with empty edges; converting the automaton with empty edges into an NFA state machine without empty edges using a preset empty edge removal algorithm; converting the edges in the NFA state machine without empty edges into a rule table composed of preset fields, merging equivalent states into one entry in the table, and then sending the rule table to the P4 switch; based on the rule table, a preset regular expression engine in the P4 switch performs pattern matching operations on the input traffic, outputting a pattern matching result when a corresponding feature is matched; and sending corresponding prompt information to a designated host based on the pattern matching result. Implementing this invention can improve the efficiency of pattern matching for input traffic and increase the space utilization during the matching process.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of programmable switch application technology, specifically relating to a regular expression pattern matching method and system based on a P4 programmable switch. Background Technology

[0002] The widespread adoption of the internet and the rapid development of information technology have brought convenience and innovation to people, but they have also brought new threats and challenges.

[0003] Regular expressions are tools used to match and process text patterns. They are an important tool in the security field, used for input validation, log analysis, threat detection, data filtering, and cleansing. They help security professionals identify malicious behavior, discover potential security issues, and strengthen the security capabilities of systems and networks.

[0004] Traditional regular expression engine implementations are primarily based on DFA or NFA. However, in the matching process of a regular expression engine implemented in software using NFA, two problems arise. First, when constructing an NFA using classic methods such as the Thompson method, empty edges can occur, which ineffectively consume space. Second, because NFA is nondeterministic, multiple states need to be accessed when processing a single character in the traffic, resulting in low memory bandwidth and slow matching speed. On the other hand, the matching process of a regular expression engine implemented in software using DFA requires a large amount of space and has a high operating cost. Summary of the Invention

[0005] To address the aforementioned problems, this invention provides a regular expression pattern matching method and system based on a P4 programmable switch, which can provide efficient and accurate regular expression matching based on a P4 programmable switch.

[0006] This application discloses a regular expression pattern matching method based on a P4 programmable switch, which includes the following steps:

[0007] The control layer obtains a set of regular expressions;

[0008] Transform the set of regular expressions into an automaton with empty edges;

[0009] The empty-edge automaton is transformed into an empty-edge-free NFA state machine by using a preset empty-edge removal algorithm.

[0010] The edges in the NFA state machine without empty edges are converted into a rule table composed of preset fields. After merging the equivalent states into one entry of the table, the rule table is sent to the P4 switch.

[0011] Based on the rule table, the preset regular expression engine in the P4 switch performs pattern matching on the input traffic and outputs the pattern matching result when the corresponding feature is matched.

[0012] Based on the pattern matching result, send the corresponding prompt message to the designated host.

[0013] In some possible implementations, the transformation process of converting the set of regular expressions into an automaton with empty edges employs the Thompson construction method.

[0014] In some possible implementations, the step of converting the automaton with empty edges into an NFA state machine without empty edges using a preset empty edge removal algorithm includes the following steps:

[0015] S31: Check the current state of the automaton. If the current state has been accessed, execute the return operation; otherwise, mark the current state as accessed.

[0016] S32: Add the current state of the automaton to the set of visited states;

[0017] S33: Check if the current state of the automaton has an ε-transition: If the current state does not have an ε-transition, then traverse all non-ε-transitions of the current state and recursively call the depth-first search function dfs_remove_epsilon for each non-ε-transition target state; if the current state has an ε-transition, then proceed to the next step.

[0018] S34: Traverse all ε-transition target states of the current state, and for each ε-transition target state, recursively call the depth-first search function dfs_remove_epsilon;

[0019] S35: Create an ε-transition list and copy all ε-transitions of the current state into the ε-transition list;

[0020] S36: Traverse each ε-transition in the ε-transition list, add all non-ε-transitions of the ε-transition state to the transitions of the current state, and retain the sign and target state of the transition; if the ε-transition state is a receiving state, mark the current state as a receiving state;

[0021] S37: Clear the ε-transition list of the current state.

[0022] In some possible implementations, the preset field is [State, char, NextState].

[0023] In some possible implementations, converting the edges in the NFA state machine without empty edges into a rule table composed of preset fields and sending it to the P4 switch includes the following steps:

[0024] Traverse each state in the NFA state machine without empty edges, and encode the edge information of each state starting from 0 and incrementing it.

[0025] Write the current state (state) and the characters char and NextState in the transition variable of the NFA state machine without empty edges into the State, char, and Action items of the rule table as an entry of the table.

[0026] After generating a rule table for each state in the NFA state machine without empty edges, the rule table is sent to the P4 switch.

[0027] In some possible implementations, based on the rule table, the preset regular expression engine in the P4 switch performs pattern matching on the input traffic, and outputs the pattern matching result when a corresponding feature is matched. The preset regular expression engine includes the following steps:

[0028] S51: In the P4 switch, two stacks, Stack0 and Stack1, are requested. Stack0 stores the current state that needs to be processed, and Stack1 stores the state that needs to be processed in the next round. Each round is defined as extracting one character from the input stream. Stack0 will start with state 0 in each round.

[0029] S52: Extract the stack top state of Stack0. If the stack top state is empty at this time, jump directly to S56.

[0030] If the stack top is not empty at this point, continue to the next step;

[0031] S53: Check whether the next character needs to be extracted according to the rule table. If the next character needs to be extracted, jump to S54; otherwise, jump to S56.

[0032] S54: After extracting the character char from the input traffic, the Action in the rule table will execute the function pushStack(NextState) to put the state to be matched in the next round into Stack1;

[0033] S55: If the Action in the rule table is Receive, it means that the pattern matching is successful. Output the matching result and end the pattern matching. Otherwise, execute the pushStack function of the Action to put NextState into Stack1, and then jump to S52.

[0034] S56: After copying the contents of Stack1 to Stack0, jump to S52.

[0035] In some possible implementations, the method of sending the corresponding prompt information to the designated host based on the pattern matching result is specifically carried out through Ethernet frames.

[0036] In some possible implementations, the step of sending corresponding prompt information to a designated host based on the pattern matching result includes: security alert information, traffic logs, etc.

[0037] Based on the above-described regular expression pattern matching method using a P4 programmable switch, this invention also proposes a system for regular expression pattern matching based on a P4 programmable switch, which includes the following modules:

[0038] The NFA logic module is used to obtain a set of regular expressions from the control layer; convert the set of regular expressions into an automaton with empty edges; and convert the automaton with empty edges into an NFA state machine without empty edges using a preset empty edge removal algorithm.

[0039] The rule table generation module is used to convert the edges in the NFA state machine without empty edges into a rule table composed of preset fields, and after merging the equivalent states into one entry of the table, the rule table is sent to the P4 switch.

[0040] The regular expression engine module includes a P4 switch, which performs pattern matching on the input traffic based on the rule table and a preset regular expression engine in the P4 switch. When a corresponding feature is matched, the module outputs the pattern matching result and sends corresponding prompt information to a specified host based on the pattern matching result.

[0041] In some possible implementations, the method of sending the corresponding prompt information to the designated host based on the pattern matching result is specifically to send it via Ethernet frames.

[0042] The regular expression pattern matching method and system based on a P4 programmable switch described in this invention have, but are not limited to, the following beneficial technical effects:

[0043] This invention leverages the high-speed characteristics of P4 programmable switches to enhance the ability to handle high-bandwidth network traffic, while simultaneously improving the speed and success rate of regular expression matching. Furthermore, the preset regular expression engine of this invention offers superior space efficiency compared to traditional methods, requiring less overall space while handling a larger number of regular expressions. Attached Figure Description

[0044] The accompanying drawings, which form part of this invention, are used to provide a further understanding of the invention. The illustrative embodiments of the invention and their descriptions are used to explain the invention and do not constitute an improper limitation of the invention.

[0045] Figure 1 This is a flowchart illustrating a regular expression pattern matching method based on a P4 programmable switch, according to an embodiment of this application.

[0046] Figure 2 This is a schematic diagram of a control layer operation process provided according to an embodiment of this application;

[0047] Figure 3 This is a schematic diagram of the operation process of a P4 switch according to an embodiment of this application;

[0048] Figure 4 This is a schematic diagram of an automaton with empty sides according to an embodiment of this application;

[0049] Figure 5 This is a schematic diagram of an NFA state machine without empty edges provided according to an embodiment of this application;

[0050] Figure 6 This is a schematic diagram of a system framework for regular expression pattern matching based on a P4 programmable switch, according to an embodiment of this application. Detailed Implementation

[0051] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present invention, and not all of them. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0052] It should be understood that, when used in this specification and the appended claims, the terms "comprising" and "including" indicate the presence of the described features, integrals, steps, operations, elements and / or components, but do not exclude the presence or addition of one or more other features, integrals, steps, operations, elements, components and / or collections thereof.

[0053] Please see Figure 1 , Figure 1 This is a flowchart illustrating a regular expression pattern matching method based on a P4 programmable switch. The method includes the following steps:

[0054] The control layer obtains a set of regular expressions;

[0055] Transform the set of regular expressions into an automaton with empty edges;

[0056] The empty-edge automaton is transformed into an empty-edge-free NFA state machine by using a preset empty-edge removal algorithm.

[0057] The edges in the NFA state machine without empty edges are converted into a rule table composed of preset fields. After merging the equivalent states into one entry of the table, the rule table is sent to the P4 switch.

[0058] Based on the rule table, the preset regular expression engine in the P4 switch performs pattern matching on the input traffic and outputs the pattern matching result when the corresponding feature is matched.

[0059] Based on the pattern matching result, send the corresponding prompt message to the designated host.

[0060] The control layer can employ the SONiC system, enabling it to translate network configuration commands into specific operational commands based on the application layer's needs. On one hand, the control layer communicates with the application layer via a northbound interface, allowing it to receive commands, obtain network status information, and adjust network behavior in real time. On the other hand, the control layer also communicates with the underlying switches via a southbound interface, which can be based on OpenFlow, P4Runtime, or similar technologies. Through this southbound interface connection, the control layer can obtain real-time device status information and issue rule configuration commands to the switches, thereby achieving management and control of the switches. This standardized interface design simplifies the development process of network applications. Within the control layer, there are typically core components such as a routing engine, policy engine, and traffic engineering module. These components work together to achieve optimized resource scheduling and flexible configuration.

[0061] Specifically, such as Figure 2-3 As shown, Figure 2 This is a schematic diagram of the control layer operation process provided in an embodiment of this application. Figure 3 This is a schematic diagram illustrating the operation of a P4 switch according to an embodiment of this application. Assuming it is necessary to detect whether there are consecutive characters "ab" in the input stream, the control layer sets the regular expression regex = ab.

[0062] For preferred options, please refer to [link / reference]. Figure 4 , Figure 4 This is a schematic diagram of an automaton with empty edges in this embodiment. The transformation process of converting the set of regular expressions into an automaton with empty edges can be performed using the Thompson construction method. State 3 is the accepting state.

[0063] Specifically, in the empty edge removal function `dfs_remove_epsilon(state)`, `state` is initialized to 0 and contains three variables: `nextstate`, `ε-transition`, and `transition`. Here, `ε-transition` and `transition` are array variables; `ε-transition` contains information about `nextstate`, and `transition` contains both `char` and `nextstate` information. Figure 2 In the control layer shown, the process of converting the automaton with empty edges into an NFA state machine without empty edges using a preset empty edge removal algorithm includes the following steps:

[0064] S31: Check the current state of the automaton. If the current state has been visited, perform the return operation; otherwise, mark the current state as visited.

[0065] S32: Add the current state of the automaton to the visited state set visited_states.

[0066] S33: Check if the current state of the automaton has an ε-transition, i.e., whether it contains a variable ε-transition. If the current state does not have an ε-transition, iterate through all non-ε-transitions of the current state, recursively calling the depth-first search function dfs_remove_epsilon for each non-ε-transition target state. In this embodiment, state = 1 at this time. If the current state has an ε-transition, proceed to the next step.

[0067] S34: Traverse all ε-transition target states of the current state, and for each ε-transition target state, recursively call the depth-first search function dfs_remove_epsilon. In this embodiment, when this step is executed, state = 2.

[0068] S35: Create an ε-transition list and copy all ε-transitions of the current state into the ε-transition list;

[0069] S36: Traverse each ε-transition in the ε-transition list, adding all non-ε-transitions of the ε-transition state to the transition of the current state, and retaining the symbol and target state of the transition; if the ε-transition state is a receiving state, then mark the current state as a receiving state. In this embodiment, state 3 and character b information are stored in nextstate and the non-ε-transition array transition in state 1.

[0070] S37: Clear the ε-transition list for the current state. At this point, as follows... Figure 5 As shown, the final result is an NFA state machine without empty edges. Figure 5 This is a schematic diagram of the NFA state machine without empty edges obtained in this embodiment, where state 2 changes to a new accepting state.

[0071] Specifically, the step of converting the edges in the NFA state machine without empty edges into a rule table composed of preset fields and sending it to the P4 switch, wherein the preset fields are [State, char, NextState], includes the following steps:

[0072] Traverse each state in the NFA state machine without empty edges, and encode the edge information of each state starting from 0 and incrementing it.

[0073] The current state (state) of the NFA state machine without empty edges, along with the characters (char) and NextState from the transition variable, are written into the State, char, and Action entries of the rule table as a table entry. The Action is represented in two ways: either it represents the action of putting NextState into Stack1 of the regular expression engine, or it represents a successful pattern match, in which case the Action is expressed as Receive.

[0074] After generating a rule table by traversing each state in the NFA state machine without empty edges, the rule table is sent to the P4 switch. In this embodiment, the table entries are 0, a, 1 and 1, b, 2.

[0075] Specifically, the preset regular expression engine in the P4 switch performs pattern matching on the input traffic, and outputs the pattern matching result when a corresponding feature is matched. The preset regular expression engine includes the following steps:

[0076] S51: In the P4 switch, two stacks, Stack0 and Stack1, are requested. Stack0 stores the current state that needs to be processed, and Stack1 stores the state that needs to be processed in the next round. Each round is defined as extracting a character from the input stream. Stack0 will start with state 0 in each round.

[0077] S52: Extract the stack top state of Stack0. If the stack top state is empty at this time, jump directly to S56. If the stack top state is not empty at this time, continue to execute the next step.

[0078] S53: Check whether the next character needs to be extracted according to the rule table. If the next character needs to be extracted, jump to S54; otherwise, jump to S56. In this embodiment, character 'a' is extracted first, and character 'b' is extracted second.

[0079] S54: After extracting the character char from the input traffic, the Action in the rule table will execute the function pushStack(NextState) to put the state to be matched in the next round into Stack1. In this embodiment, the state to be matched in the next round is 0 and 1.

[0080] S55: If the Action in the rule table is Receive, it means that the pattern matching is successful. Output the matching result and end the pattern matching. Otherwise, execute the pushStack function of the Action to put NextState into Stack1, and then jump to S52.

[0081] S56: After copying the contents of Stack1 to Stack0, proceed to S52. In this embodiment, 0 and 1 are copied to Stack0.

[0082] Please see Figure 6 , Figure 6 This is a schematic diagram of a regular expression pattern matching system based on a P4 programmable switch. The system includes the following specific modules:

[0083] The NFA logic module is used to obtain a set of regular expressions from the control layer; convert the set of regular expressions into an automaton with empty edges; and convert the automaton with empty edges into an NFA state machine without empty edges using a preset empty edge removal algorithm.

[0084] The rule table generation module is used to convert the edges in the NFA state machine without empty edges into a rule table composed of preset fields, merge the equivalent states into one entry of the table, and then send the rule table to the P4 switch.

[0085] The regular expression engine module includes a P4 switch, which performs pattern matching on the input traffic based on the rule table and a preset regular expression engine in the P4 switch. When a corresponding feature is matched, the module outputs the pattern matching result and sends corresponding prompt information to a specified host based on the pattern matching result.

[0086] Specifically, the method for sending the corresponding prompt information to the designated host based on the pattern matching result is to send it via Ethernet frames.

[0087] The preferred embodiments of the present invention have been described in detail above. It should be understood that the steps in the methods of the embodiments of the present invention can be adjusted, combined, or deleted according to actual needs. Furthermore, it should be understood that those skilled in the art can make many modifications and variations based on the concept of the present invention without creative effort. Therefore, all technical solutions that can be obtained by those skilled in the art based on the concept of the present invention through logical analysis, reasoning, or limited experimentation on the basis of the prior art should be within the scope of protection defined by the claims.

Claims

1. A regular expression pattern matching method based on a P4 programmable switch, characterized in that, The method includes the following steps: The control layer obtains a set of regular expressions; Transform the set of regular expressions into an automaton with empty edges; The empty-edge automaton is transformed into an empty-edge-free NFA state machine by using a preset empty-edge removal algorithm. The edges in the NFA state machine without empty edges are converted into a rule table composed of preset fields. After merging the equivalent states into one entry of the table, the rule table is sent to the P4 switch. Based on the rule table, the preset regular expression engine in the P4 switch performs pattern matching on the input traffic and outputs the pattern matching result when the corresponding feature is matched. Based on the pattern matching result, send the corresponding prompt message to the designated host; The process of converting the automaton with empty edges into an NFA state machine without empty edges using a preset empty edge removal algorithm includes the following steps: S31: Check the current state of the automaton. If the current state has been accessed, perform the return operation; otherwise, mark the current state as accessed. S32: Add the current state of the automaton to the set of visited states; S33: Check if the current state of the automaton has an ε-transition: If the current state does not have an ε-transition, then traverse all non-ε-transitions of the current state and recursively call the depth-first search function dfs_remove_epsilon for each non-ε-transition target state; if the current state has an ε-transition, then proceed to the next step. S34: Traverse all ε-transition target states of the current state, and for each ε-transition target state, recursively call the depth-first search function dfs_remove_epsilon; S35: Create an ε-transition list and copy all ε-transitions of the current state into the ε-transition list; S36: Traverse each ε-transition in the ε-transition list, add all non-ε-transitions of the ε-transition state to the transitions of the current state, and retain the sign and target state of the transition; if the ε-transition state is a receiving state, mark the current state as a receiving state; S37: Clear the ε-transition list of the current state; Based on the rule table, the preset regular expression engine in the P4 switch performs pattern matching on the input traffic, and outputs the pattern matching result when a corresponding feature is matched. The preset regular expression engine includes the following steps: S51: In the P4 switch, two stacks, Stack0 and Stack1, are requested. Stack0 stores the current state that needs to be processed, and Stack1 stores the state that needs to be processed in the next round. Each round is defined as extracting one character from the input stream. Stack0 will start with state 0 in each round. S52: Extract the stack top state of Stack0. If the stack top state is empty at this time, jump directly to S56. If the stack top state is not empty at this time, continue to execute the next step. S53: Check whether the next character needs to be extracted according to the rule table. If the next character needs to be extracted, jump to S54; otherwise, jump to S56. S54: After extracting the character char from the input traffic, the Action in the rule table will execute the function pushStack(NextState) to put the state to be matched in the next round into Stack1; S55: If the Action in the rule table is Receive, it means that the pattern matching is successful. Output the matching result and end the pattern matching. Otherwise, execute the pushStack function of the Action to put NextState into Stack1, and then jump to S52. S56: After copying the contents of Stack1 to Stack0, jump to S52.

2. The regular expression pattern matching method based on a P4 programmable switch according to claim 1, characterized in that, The process of converting the set of regular expressions into an automaton with empty edges uses the Thompson construction method.

3. The regular expression pattern matching method based on a P4 programmable switch according to claim 1, characterized in that, The preset fields are [State, char, NextState].

4. The regular expression pattern matching method based on a P4 programmable switch according to claim 3, characterized in that, The process of converting the edges in the NFA state machine without empty edges into a rule table composed of preset fields and sending it to the P4 switch includes the following steps: Traverse each state in the NFA state machine without empty edges, and encode the edge information of each state starting from 0 and incrementing it. Write the current state (state) and the characters char and NextState in the transition variable of the NFA state machine without empty edges into the State, char, and Action items of the rule table as an entry of the table. After generating a rule table for each state in the NFA state machine without empty edges, the rule table is sent to the P4 switch.

5. The regular expression pattern matching method based on a P4 programmable switch according to claim 1, characterized in that, The method for sending the corresponding prompt information to the designated host based on the pattern matching result is specifically to send it via Ethernet frames.

6. The regular expression pattern matching method based on a P4 programmable switch according to claim 1, characterized in that, Based on the pattern matching result, a corresponding prompt message is sent to the designated host, including a security alarm message.

7. A regular expression pattern matching system based on a P4 programmable switch, characterized in that, The system is used to execute the regular expression pattern matching method based on a P4 programmable switch as described in claim 1, and includes: The NFA logic module is used to obtain a set of regular expressions from the control layer; convert the set of regular expressions into an automaton with empty edges; and convert the automaton with empty edges into an NFA state machine without empty edges using a preset empty edge removal algorithm. The rule table generation module is used to convert the edges in the NFA state machine without empty edges into a rule table composed of preset fields, and after merging the equivalent states into one entry of the table, the rule table is sent to the P4 switch. The regular expression engine module includes a P4 switch, which performs pattern matching on the input traffic based on the rule table and a preset regular expression engine in the P4 switch. When a corresponding feature is matched, the module outputs the pattern matching result and sends corresponding prompt information to a specified host based on the pattern matching result.

8. A regular expression pattern matching system based on a P4 programmable switch according to claim 7, characterized in that, The method for sending the corresponding prompt information to the designated host based on the pattern matching result is specifically to send it via Ethernet frames.