A general method for calculating the valid field of forwarding rules
By using a ternary radix tree data structure to store forwarding rules, the problem of calculating the effective field for matching arbitrary wildcard patterns in network devices is solved, achieving efficient calculation of the effective field of forwarding rules and improved compatibility.
Patent Information
- Application Number
- CN202511263490.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-05
- Publication Date
- 2025-10-31
- Estimated Expiration
- 2045-09-05
AI Technical Summary
Existing technologies struggle to efficiently calculate the effective domain of forwarding rules under arbitrary wildcard patterns in network devices, and prefix tree structures cannot adapt to more general wildcard pattern matching requirements.
The forwarding rules are stored using a ternary radix tree data structure. Through insertion, deletion and query operations, the matching query of any wildcard pattern is realized, and the effective domain of the forwarding rules is calculated through the ternary radix tree.
It enables fast calculation of valid fields under any wildcard pattern, improving the compatibility and efficiency of network devices, and is applicable to prefix form and more general wildcard pattern matching.
Smart Images

Figure CN120750849B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of network device configuration technology, and in particular to a general method for calculating the effective domain of forwarding rules. Background Technology
[0002] In network devices, the effective field of each forwarding rule refers to the set of data packets that the rule can match during the actual forwarding process.
[0003] The traditional calculation method is to subtract the matching sets of all rules with higher priority that intersect with the matching set of the rule from the matching set of the rule.
[0004] To accelerate this process, existing technologies typically use data structures such as prefix trees to optimize prefix matching. However, such structures are only suitable for matching prefix forms (such as IP prefixes) and are difficult to adapt to the more general wildcard pattern matching requirements. Summary of the Invention
[0005] To address the aforementioned technical problems in the existing technology, this invention provides a general method for calculating the effective field of forwarding rules, the technical solution of which includes:
[0006] Step S1: Insert all forwarding rules into the ternary radix tree based on the matching;
[0007] Step S2: Query all matches in the ternary cardinality tree that intersect with forwarding rule M, and determine the forwarding rule corresponding to each match to form a rule set;
[0008] In this case, the priority of forwarding rule M is P;
[0009] Step S3: Subtract the matches corresponding to all forwarding rules with higher priority than P in the rule set from the matches corresponding to forwarding rule M to obtain the effective domain of forwarding rule M;
[0010] Among them, forwarding rules are the rules in network devices that determine whether to receive a certain data packet and determine the target forwarding location of the received data packet.
[0011] Compared with existing technologies, the technical solution provided by this invention is not only compatible with prefix matching, but also can quickly calculate the effective field under any wildcard mode, and has good compatibility; the provided ternary radix tree can efficiently support intersection matching queries under any wildcard mode, and also has good compatibility. Attached Figure Description
[0012] Figure 1 This is a schematic diagram of the tree structure for storing the forwarding table in a radix tree.
[0013] Figure 2This is a schematic diagram of the structure for storing five matching ternary radix trees in one embodiment of the present invention.
[0014] Figure 3 This is a schematic diagram of the state of a ternary radix tree after multiple insertions and deletions in one embodiment of the present invention.
[0015] Figure 4 This is a flowchart of a general calculation method for the valid domain of a rule in one embodiment of the present invention. Detailed Implementation
[0016] The technical solutions provided by the present invention will be further described in detail below with reference to the embodiments and accompanying drawings.
[0017] Example 1:
[0018] In computer networks, data packet forwarding is based on forwarding rules, which match and trigger corresponding forwarding actions. The matching field often consists of a binary string and a wildcard "*", called wildcard pattern matching. If the wildcard only appears consecutively at the end of the string (e.g., 011**), it's called prefix matching; if the matching string contains no wildcard, it's called a complete match (e.g., 01100); if a wildcard is followed by specific bits (e.g., 01*1*), it's called general wildcard pattern matching. In modeling and analyzing the forwarding behavior of network devices, the effective field of each rule needs to be calculated. The effective field of a rule is defined as the set of data packets that actually match that rule on the network device. Therefore, "match" is actually a character representation of a network address range. Forwarding rules refer to a set of pre-defined conditions and actions in network devices (such as routers, firewalls, switches, or load balancers) that can judge each data packet and decide whether to receive it. Furthermore, for already received data packets, it can determine how to process or forward them to the target forwarding location.
[0019] As shown in Table 1, the matching set of the third rule in the table is the entire 0.0.0.0 / 1 network segment. However, since the first rule has a higher priority and overlaps (covering 64.0.0.0 / 2), the effective field of the third rule should be 0.0.0.0 / 1 minus 64.0.0.0 / 2; the effective field of the fourth rule needs to be the matching set of the first three higher priority rules.
[0020] Table 1 Example of Device Forwarding
[0021] .
[0022] If the intersection relationship between rules and matches is unknown, the method to calculate the effective domain of a rule is to subtract all rule matches with higher priority from the matches of that rule. The time complexity is O(N), where N is the number of rules with higher priority than the forwarding rule. In practical applications, to avoid N calculations, such as... Figure 1As shown, prefix trees and similar data structures are often used to optimize the storage of prefix matching. The prefix inclusion property can be used to obtain the set of intersection rules by traversing the prefix path.
[0023] However, prefix tree data structures can only store prefix matches. In reality, many matching scenarios in computer networks require more general wildcard pattern matching, such as IoT device IDs, MAC addresses, and IPv6 addresses. In these scenarios, different bit regions of the ID / address string have specific meanings. Taking IoT device IDs as an example, its format is typically:
[0024] "[Region: 4 digits][Batch: 4 digits][Type: 4 digits][Serial Number: 4 digits]"
[0025] If you need to match a certain type of IoT device, the wildcard pattern matching might be as follows:
[0026] "[****][****]
[1010] [****]".
[0027] To store more general wildcard pattern matches, this embodiment 1 proposes a ternary radix tree data structure based on extended wildcards for storing wildcard pattern matches, providing insertion, deletion, and query interfaces. Furthermore, based on this data structure, a general and fast method for calculating the valid domain of forwarding rules is proposed.
[0028] First, wildcard pattern matching is modeled. Undoubtedly, the word "matching" appearing alone in the following text refers to "wildcard pattern matching".
[0029] In this embodiment, a matching common prefix is defined as follows: A matching common prefix is the string remaining after removing consecutive trailing asterisks (*). For example, the common prefixes of 011**, 01100, and 01*1* are 011, 01100, and 01*1, respectively. The common common prefix of two matches is a continuous substring starting from the first character and present in both matching common prefixes. For example, the common common prefixes of 011** and 01*1* are 0 and 01. The longest of the common common prefixes is the longest common common prefix.
[0030] 1. Triadic radix tree;
[0031] Based on the concept of a common prefix, this embodiment provides a ternary radix tree. The node content of a ternary radix tree consists of three fields: segment, match, and intersection set. Each node also has three pointers representing 0, 1, and *, pointing to three children: 0-child nodes, 1-child nodes, and *-child nodes. The segment is a substring within a match, and the intersection set consists of all matches in the subtree rooted at that node that intersect with the match stored at that node. Specifically, the segment of the current node is the substring obtained by subtracting the longest common common prefix of the current node and its parent nodes from the common prefix of the current node, and the segment of the root node is its own common prefix.
[0032] like Figure 2 As shown, the ternary radix tree stores five matches, which are stored as 0****, 01***, 010**, 01*1*, and 011** in nodes 1 through 5, respectively. Each match in the ternary radix tree has a unique node storing it. The ternary radix tree guarantees that, given a node that stores a match, there exists a unique path from the root node to that node, and the concatenation of the segments from all nodes along the path (including the first and last nodes) forms the common prefix of the match stored at that node. For example, the path from node 1 to node 4 passes through nodes 1, 2, and 4, with segments 0, 1, and *1 respectively. The concatenation result 01*1 is the common prefix of the match stored at node 4. Each node in the ternary radix tree stores all matches in the subtree rooted at that node that intersect with the match stored at that node.
[0033] 2. Insertion into a ternary radix tree;
[0034] The insertion operation of a ternary radix tree inserts a match into the ternary radix tree. The recursive algorithm for this operation is described below:
[0035] Input: match, the common prefix for the match, and root node;
[0036] Output: The new root node.
[0037] Step 1: If the root node is empty, create a new node. Set the new node's match to `match`, its segment to the common prefix `prefix`, initialize the intersection set to empty, and return the new node. If the root node is not empty, calculate the longest common prefix (LCP) between the common prefix and the segment content of the root node.
[0038] Step 2: Perform the following operations based on the length of the longest common prefix (LCP):
[0039] If the length of the longest common prefix (lcp) is equal to the segment length of the root node (root), then the match should be inserted into the subtree rooted at the root node (root). Let the current node be the node newRoot.
[0040] Otherwise, insert a new node before the root node, set its segment to the longest common prefix (lcp), set the match to null, and inherit the intersection set of the root node. Simultaneously, update the segment of the root node to the remaining portion after removing the lcp, and set the root node as a child of the new node. Denote this new node as node newRoot.
[0041] Step 3: Add the match to the intersection of node newRoot.
[0042] Step 4: Perform the following operations based on the length of the common prefix and the length of the longest common common prefix (lcp):
[0043] If the length of the common prefix is equal to the length of the longest common prefix (lcp), it means that the node newRoot is the node that stores the match. Set the match of node newRoot as the match and return node newRoot.
[0044] Otherwise, remove the longest common common prefix (lcp) from the common prefix, update the common prefix, and select the child pointer childNode corresponding to the node newRoot based on the first character (0, 1, or *) of the updated common prefix.
[0045] Recursively execute steps 1 to 4, taking the input match, the common prefix, and the child pointer childNode, to obtain a new child pointer newChildNode, and update the child pointer of node newRoot. Return the set node newRoot.
[0046] 3. Deletion of a ternary radix tree;
[0047] The deletion operation in a ternary radix tree removes a match from the tree. Its recursive algorithm is described below:
[0048] Input: match, common prefix, root node;
[0049] Output: The new root node.
[0050] Step 1: If the root node is empty, return empty directly.
[0051] Step 2: If the segment of the root node is not the prefix of the common prefix, it means that there is no match in the ternary radix tree, and the root node is returned directly.
[0052] Step 3: Remove the match from the intersection set of the root node.
[0053] Step 4: Perform the following operations based on the length of the common prefix and the segment length of the root node:
[0054] If the length of the generic prefix equals the segment length of the root node, it means that the root node stores the nodes that match the match. Set the match value of the root node to null, and then perform the following operations based on the three child nodes of the root node:
[0055] Empty: Deletes the root node and returns an empty string;
[0056] If there is only one non-empty child node: Move the segment of the root node to the beginning of the segment of the only child node, delete the root node, and return the child node.
[0057] If two or three child nodes are not empty: return the root node directly.
[0058] Otherwise, it means the target node is in a descendant node. After removing the root node segment from the general prefix, update the general prefix. Select the child pointer childNode corresponding to the root node based on the first character (0, 1, or *) of the updated general prefix.
[0059] Recursively execute steps 1 to 4, taking the input match, the common prefix, and the child pointer childNode, to obtain the child pointer newChildNode, and update the child pointer of the root node root. Return the root node root.
[0060] 4. Queries in a ternary radix tree;
[0061] The query operation is used to retrieve all matches that intersect with a given match. Its recursive algorithm is described as follows:
[0062] Input: match, common prefix, root node;
[0063] Output: The set of elements that are matched.
[0064] Step 1: If the root node is empty, return an empty set. If not empty, let rootLen be the length of the segment of the root node.
[0065] Step 2: Compare the rootLen bit before the general prefix with the segment of the root node: If a conflict occurs (one bit is 0 and the other is 1), return an empty set.
[0066] Step 3: If the length of the general prefix is less than or equal to rootLen, return the intersection of the root node root.
[0067] Step 4: Create a set. If the root node contains a match, add it to the set; after removing the segment from the root node in the common prefix, update the common prefix based on the first character of the updated common prefix.
[0068] Recursively execute steps 1 to 4, inputting the match, the common prefix, and the *child nodes of the root node, and add the set returned by the algorithm to the set.
[0069] If the first character of the updated general prefix is 0: recursively execute steps 1 to 4, input the match match, the general prefix and the 0 child node of the root node, add the set0 returned by the algorithm to the set set, and return the set set.
[0070] If the first character of the updated general prefix is 1: recursively execute steps 1 to 4, input the match match, the general prefix and the 1 child node of the root node, add the set1 returned by the algorithm to the set set, and return the set set.
[0071] If the first character of the updated generic prefix is *: recursively execute steps 1 to 4 twice, with the input parameters being the parameters when the first character is 0 and 1 respectively, and obtain set0 and set1 at the same time. Add all of them to set and return set.
[0072] An example showing the state of a ternary radix tree after multiple insertions and deletions is as follows: Figure 3 As shown.
[0073] 5. A general and fast method for calculating the valid domain of forwarding rules;
[0074] Based on the ternary radix tree, this embodiment proposes a general calculation method for the effective domain of forwarding rules.
[0075] Before computation, the insertion operation is first used to insert all forwarding rules into the tree based on the matching of the forwarding rules, and to maintain a one-to-one mapping relationship between the matching and the forwarding rules.
[0076] The general calculation method for a forwarding rule M with priority P is described as follows: Figure 4 As shown.
[0077] Step 1: Using the query operation, query all matching sets in the ternary cardinality tree that intersect with the forwarding rule M, and obtain the rule set through the mapping relationship (that is, determine the forwarding rule corresponding to each match in the matching set, which constitutes the rule set).
[0078] Step 2: Subtract the matches of rules with higher priority than P from the matching of forwarding rule M in sequence. The forwarding rule M after the subtraction operation is the valid domain of forwarding rule M.
[0079] As can be seen from the above embodiments and accompanying drawings, the technical solution provided by the present invention overcomes the limitations of the prior art. It is not only suitable for prefix matching, but also can quickly realize the calculation of the effective field under any wildcard mode, and has good compatibility. The provided ternary radix tree can efficiently support intersection matching queries under any wildcard mode, and also has good compatibility.
Claims
1. A general method for calculating the valid field of a forwarding rule, characterized in that, Includes the following steps: Step S1: Insert all forwarding rules into the ternary radix tree based on the matching; Step S2: Query all matches in the ternary cardinality tree that intersect with forwarding rule M, and determine the forwarding rule corresponding to each match to form a rule set; In this case, the priority of forwarding rule M is P; Step S3: Subtract the matches corresponding to all forwarding rules with higher priority than P in the rule set from the matches corresponding to forwarding rule M to obtain the effective domain of forwarding rule M; Among them, the forwarding rules are the rules in network devices that determine whether to receive a certain data packet and determine the target forwarding location of the received data packet; The ternary radix tree includes: Each node includes three fields: segment, match, and intersection, and three pointers representing 0, 1, and * respectively; among them, The segment of the current node is the substring obtained by removing the longest common common prefix of the current node and its parent node from the common prefix of the current node; the segment of the root node is its own common prefix. The intersection set includes all matches in the subtree rooted at the current node that intersect with the match in the root node; the three pointers point to the three nodes one-to-one. It also includes a method for inserting the match into a ternary radix tree: (1) If the root node of the ternary radix tree is empty, create a new node; set the match of the new node to match, set the segment to the common prefix of match prefix, set the intersection set to empty, and return the new node; (2) If the root node of the ternary radix tree is not empty, calculate the longest common common prefix (lcp) between the common prefix and the middle segment of the root node; If the length of the longest common prefix (lcp) is equal to the length of the middle segment of the root node, then the match will be inserted into the subtree where the root node is the root node, and the current node will be recorded as the node newRoot. Add the match to the intersection of node newRoot; If the longest common prefix (lcp) and the common prefix (prefix) are of equal length, then set the match of node newRoot as match and return node newRoot; Otherwise, remove the longest common common prefix (lcp) from the common prefix and update the common prefix; select the child node corresponding to the node newRoot based on the first character of the updated common prefix; Recursively calculate the new root node using the child node as the child node and the updated general prefix as the general prefix for matching; replace the child node with the obtained child node newChildNode; and return the node newRoot.
2. The general calculation method for the valid domain of a forwarding rule as described in claim 1, characterized in that, It also includes a method for removing the match from the ternary radix tree: (1) If the root node is empty, return an empty node; (2) If the segment of the root node is not a common prefix, then return the root node; Remove the match from the intersection set of the root node; If the length of the common prefix and the length of the root node segment are equal, then the matching of the root node is set to null, including: 1) If all three child nodes of the root node are empty, delete the root node and return an empty node; 2) If the root node has only one non-empty child node, then move the segment of the previous root node to the front of the segment of the non-empty child node, delete the root node, and return the non-empty child node. 3) If the root node has at least two non-empty child nodes, then return the root node. Otherwise, remove the segment of the root node from the general prefix and update the general prefix; select the child node corresponding to the root node based on the first character of the updated general prefix. Recursively calculate the common prefix for matching, using the child node as the new root node and the updated common prefix as the common prefix for matching; replace the child node with the obtained child node newChildNode; and return the root node.
3. The general calculation method for the valid domain of a forwarding rule as described in claim 1, characterized in that, It also includes a method for querying the intersection of the match in a ternary radix tree: (1) If the root node is empty, return an empty set; (2) If the root node is not empty, compare the first rootLen bits of the general prefix with the segment of the root node; where rootLen is the length of the segment in the root node. If a conflict occurs, return an empty set; If the length of the general prefix is less than or equal to rootLen, then return the intersection of the root node root; If the root node contains a match, then add the match of the root node to the set. Remove the root node segment from the common prefix and update the common prefix; Using the *child node of the root node as the new root node, and the common prefix as the common prefix for matching, perform recursive calculations and add the returned set to the set. If the first character of the general prefix is 0, then the 0th child node of the root node is taken as the new root node, the general prefix is taken as the general prefix for matching, the calculation is performed recursively, and the returned set is added to the set set, and the set set is returned. If the first character of the general prefix is 0, take the 1 child node of the root node as the new root node, take the general prefix as the general prefix that matches the match, perform recursive calculation, add the returned set to the set, and return the set. If the first character of the general prefix is *, then the 0th and 1st child nodes of the root node are taken as the new root nodes respectively, and the general prefix is taken as the general prefix for matching. The recursive calculation is performed respectively, and the sets returned by each are added to the set set, and the set is returned.
Citation Information
Patent Citations
Improved method of adaptive radix tree supporting any Key value
CN113626432A
Routing forwarding method for virtual private network
CN116319555A