A method and device for matching arbitrary range rules based on TCAM
By generating TCAM entries through a greedy iterative decomposition strategy, the alignment range limitation of a single TCAM entry is resolved. This achieves arbitrary range rule matching with zero hardware cost, lossless line-speed performance, controllable number of entries, and lossless matching accuracy, and is applicable to multiple types of numerical fields.
Patent Information
- Application Number
- CN202611362658.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-03
- Publication Date
- 2026-11-27
AI Technical Summary
Existing TCAM single entries can only express power-of-2 aligned intervals. Existing range decomposition schemes suffer from high hardware overhead, mismatched alignment methods, or lack of dynamic adaptation capabilities, and cannot efficiently decompose arbitrary numerical intervals into TCAM entries.
A greedy iterative decomposition strategy is adopted to generate TCAM entries that satisfy length and alignment constraints through iteration. By utilizing the existing Key-Mask matching mechanism, the union of the generated sub-intervals is strictly equal to the original interval, and no additional hardware or modification to the TCAM structure is required.
It achieves arbitrary range rule matching with zero hardware cost, lossless line-speed performance, controllable number of table entries, and lossless matching accuracy, and is suitable for range search of multiple types of numerical fields.
Smart Images

Figure CN123185138A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of network communication and chip data forwarding technology, specifically relating to a method and apparatus for arbitrary range rule matching based on a ternary content addressable memory (TCAM), applicable to range rule lookup for various numerical fields such as Transmission Control Protocol port range, User Datagram Protocol port range, Internet Protocol version 4 address range, Internet Protocol version 6 address range, and Virtual LAN identifier range. Background Technology
[0002] Tri-state content-addressable memory (TCAM) is widely used in high-speed rule lookup in network processors and switching chips due to its hardware feature of comparing all entries in parallel within a single clock cycle. Each memory cell of TCAM has three states: 0, 1, and "don't care". By configuring the key and mask, TCAM can complete the parallel comparison of all entries within a single clock cycle. The lookup latency is independent of the number of entries, making it a key hardware component for achieving line-speed forwarding.
[0003] The fundamental limitation of TCAM is that a single entry can only represent a numerical range of the form [K, K+2^n-1], where K must be an integer multiple of 2^n, i.e., a power-of-2 aligned range. For any unaligned range—such as the port range [1000, 1025] or the Internet Protocol version 4 address range [192.168.1.100, 192.168.1.200]—a single TCAM entry cannot directly cover it, and the range must be decomposed into multiple entries.
[0004] In the prior art, patent US9299433B1 proposes a two-level lookup architecture. This scheme uses a TCAM to store TCAM-friendly ranges, and then stores the upper and lower limits of each range in static random access memory (SRAM) and performs boundary verification by a comparator. Although this scheme can achieve multi-range lookup, it does not solve the pre-encoding problem of how to convert arbitrary ranges into TCAM-friendly ranges. The ranges stored in the TCAM still need to be given in advance in a TCAM-friendly format, and this scheme requires additional SRAM and comparator hardware.
[0005] Patent application US20140268971A1 proposes a TCAM range search method based on block-based recursive decomposition. This scheme divides a W-bit range field into multiple blocks, each of size B bits, and uses a recursive decomposition method to process each bit downwards starting from the second-highest bit. However, this scheme's decomposition is based on block boundary alignment rather than power-of-2 alignment, and because it uses a recursive decomposition path, it lacks a mechanism to dynamically adjust the power to adapt to different initial value alignment requirements.
[0006] Patent CN103546378B proposes a two-level hierarchical search method for range multi-matching based on TCAM. This scheme achieves range matching by extracting wide interval ranges from the range set and merging associated ranges. However, this scheme is designed for multi-matching scenarios, and its range division is based on the correlation between ranges rather than mathematical optimal decomposition. Furthermore, the scheme does not provide a theoretical upper limit on the number of entries.
[0007] Furthermore, range-to-prefix expansion is a classic method for TCAM range matching, which stores the range by splitting it into a set of prefixes. However, this method uses a static enumeration of the prefix set and lacks the ability to dynamically adapt to the starting value of the interval.
[0008] In summary, existing technologies fail to simultaneously meet the following requirements: no additional hardware required, no modification to the TCAM structure, support for arbitrary numerical field types, controllable number of entries with a theoretical upper bound, and a decomposition method that can dynamically adapt to the starting value characteristics of different intervals. Therefore, a general method is urgently needed to efficiently decompose any continuous numerical interval into TCAM entries. Summary of the Invention
[0009] The present invention aims to provide an arbitrary range rule matching method and apparatus based on TCAM, thereby solving the problems that existing TCAM single entries can only express alignment intervals of powers of 2, and that existing range decomposition schemes have defects such as large hardware overhead, mismatched alignment methods, or lack of dynamic adaptation capabilities.
[0010] To achieve the above objectives, the present invention provides an arbitrary range rule matching method based on TCAM, the method comprising: Obtain the target numerical range to be matched, wherein the target numerical range has a start value, an end value, and a bit width parameter; Using the initial value as the current starting value, iteratively perform the sub-interval generation operation until the current starting value is greater than the ending value; The sub-interval generation operation includes: determining a candidate power based on the current starting value and the ending value, wherein the candidate power is the largest integer n satisfying that 2^n does not exceed the remaining length of the target value interval; verifying whether the current starting value satisfies the alignment condition cur mod 2^n = 0; if the current starting value does not satisfy the alignment condition, decreasing the candidate power until the current starting value satisfies the alignment condition; generating a TCAM entry based on the current starting value and the candidate power satisfying the alignment condition, wherein the entry contains a key and a mask, the key being equal to the current starting value, and the mask being determined based on the candidate power and the bit width parameter; and updating the current starting value to cur = cur + 2^n. Write each generated TCAM entry into the TCAM.
[0011] In one embodiment of this method, the mask is determined as follows: the bitwise inverse of 2^n - 1 is calculated, and then the bitwise inverse is ANDed with 2^W - 1, where W is the bit width parameter.
[0012] In one embodiment of this method, the method further includes: after writing to the TCAM, in response to receiving the input value to be searched, using the input value as a search keyword and comparing it in parallel with each entry in the TCAM; if at least one entry matches, then determining that the input value falls within the target value range.
[0013] In one embodiment of this method, the field type of the target value range is any one of the following: Transmission Control Protocol port field, User Datagram Protocol port field, Internet Protocol version 4 address field, Internet Protocol version 6 address field, and Virtual Local Area Network identifier field.
[0014] In one embodiment of this method, the bit width parameter is determined according to the field type of the target numerical range, and the method uses the same iterative decomposition logic for any field type.
[0015] To achieve the above objectives, the present invention also provides an arbitrary range rule matching device based on TCAM, the device comprising: The interval acquisition module is configured to acquire the target numerical interval to be matched, wherein the target numerical interval has a start value, an end value, and a bit width parameter; The iterative decomposition module is configured to iteratively perform a sub-interval generation operation with the initial value as the current starting value until the current starting value is greater than the ending value. The sub-interval generation operation includes: determining a candidate power based on the current starting value and the ending value, wherein the candidate power is the largest integer n that satisfies 2^n not exceeding the remaining length of the target value interval; verifying whether the current starting value satisfies the alignment condition cur mod 2^n = 0; if the current starting value does not satisfy the alignment condition, decreasing the candidate power until the current starting value satisfies the alignment condition; generating a TCAM entry based on the current starting value and the candidate power that satisfies the alignment condition, wherein the entry contains a key and a mask, the key being equal to the current starting value, and the mask being determined based on the candidate power and the bit width parameter; and updating the current starting value to cur = cur + 2^n. The table entry writing module is configured to write each generated TCAM table entry into the TCAM.
[0016] In one embodiment of this device, the iterative decomposition module determines the mask in the following manner: calculate the bitwise inversion result of 2^n - 1, and then perform a bitwise AND operation between the bitwise inversion result and 2^W - 1, where W is the bit width parameter.
[0017] In one embodiment of this device, the device further includes: The parallel matching module is configured to, in response to receiving an input value to be searched, use the input value as a search keyword and compare it in parallel with each entry in the TCAM, and determine that the input value falls within the target value range when at least one entry matches.
[0018] In one embodiment of this device, the field type of the target value range is any one of the following: Transmission Control Protocol port field, User Datagram Protocol port field, Internet Protocol version 4 address field, Internet Protocol version 6 address field, and Virtual Local Area Network identifier field.
[0019] In one embodiment of this device, the bit width parameter is determined according to the field type of the target value range, and the iterative decomposition module uses the same iterative decomposition logic for any field type.
[0020] Compared with the prior art, the present invention has the following beneficial effects: First, zero hardware cost. This invention fully utilizes the existing TCAM key-mask matching mechanism to achieve accurate coverage of any interval without adding any additional circuitry such as range comparators, auxiliary memory, or modifying the TCAM unit structure. Compared to the US9299433B1 solution, which requires additional static random access memory and comparators, this invention has a significant advantage in terms of hardware resource consumption.
[0021] Second, line-speed performance remains lossless. The parallel lookup latency of TCAM is independent of the number of entries; range decomposition is completed during the rule issuance phase, and the small number of entries added during data plane lookups do not introduce any additional clock cycle delay. Therefore, the lookup performance of this invention is exactly the same as that of a single exact match rule.
[0022] Third, the number of entries is controllable. This invention employs a greedy iterative decomposition strategy, generating at most 2W-2 TCAM entries for any W-bit interval [min, max], far fewer than the max-min+1 entries required by the hash exhaustive search scheme. Compared to existing prefix extension techniques that also generate 2W-2 entries in the worst case but use a static enumeration method, this invention achieves the same upper bound through dynamic greedy iteration, resulting in clearly predictable storage resource usage.
[0023] Fourth, the matching accuracy is lossless. The union of each sub-interval obtained by the decomposition in this invention is strictly equal to the original interval [min, max], with neither false negatives nor false positives. This lossless accuracy characteristic comes from the complete mathematical closed loop between mask calculation and greedy decomposition algorithm.
[0024] Fifth, the field types are universal. This invention uses the bit width parameter W as the only field type adaptation parameter. The same algorithm core logic can adapt to any numerical field type, such as Transmission Control Protocol port, User Datagram Protocol port, Internet Protocol version 4 address, Internet Protocol version 6 address, and Virtual LAN identifier, without any modification. Attached Figure Description
[0025] The present invention will be further described below with reference to the accompanying drawings and specific embodiments.
[0026] Figure 1 This is a schematic diagram of the overall process of the arbitrary range rule matching method based on TCAM of the present invention; Figure 2 This is a detailed flowchart of the interval iterative decomposition algorithm of the present invention; Figure 3 This is a schematic diagram of the system architecture of the arbitrary range rule matching device based on TCAM of the present invention; Figure 4This is a schematic diagram illustrating the iterative decomposition effect of the interval [1000, 1025] in this invention; Figure 5 This is a schematic diagram of the iterative decomposition effect of the interval [6881, 6889] in this invention. Detailed Implementation
[0027] To make the technical means, creative features, objectives and effects of this invention easier to understand, the invention will be further described below with reference to specific illustrations.
[0028] This invention addresses the inherent limitation of TCAM, where a single entry can only represent power-of-2 aligned intervals, by providing a general numerical range matching solution based on a greedy iterative decomposition strategy. This solution requires no modification to the TCAM hardware structure, no additional comparators or auxiliary memory, and fully utilizes TCAM's existing keyword-mask matching mechanism to achieve accurate coverage of any continuous interval.
[0029] Specifically, this invention first constructs a bidirectional constraint power selection mechanism. In each iterative decomposition, this invention simultaneously applies two conditions: a length constraint and an alignment constraint. The length constraint ensures that the current sub-interval does not exceed the range of the remaining interval to be covered, i.e., 2^n <= L, where L is the length of the remaining interval; the alignment constraint ensures that the initial value of the current sub-interval meets the alignment requirements of a single TCAM entry, i.e., cur mod 2^n = 0. When a candidate power does not meet the alignment constraint, this invention decreases n until the alignment condition is met. This bidirectional constraint mechanism is fundamentally different from the recursive decomposition scheme based on a fixed block size in US20140268971A1, which relies on block boundary alignment rather than power-of-2 alignment, and lacks a mechanism for dynamically adjusting the power to adapt to alignment requirements.
[0030] Secondly, this invention constructs an iterative advancement architecture based on a greedy strategy. This invention adopts a left-to-right iterative advancement method. In each iteration, the maximum power satisfying the bidirectional constraints is selected at the current starting value to generate the corresponding entry. Then, the starting value is advanced forward, i.e., cur = cur + 2^n, and the above process is repeated on the reduced remaining interval. This greedy strategy is fundamentally different from the static prefix enumeration or range-based merging and partitioning schemes in the prior art. The dynamic decision-making mechanism of this invention enables the adaptive generation of optimal decomposition results for intervals of different positions and lengths.
[0031] Furthermore, this invention achieves closed-loop collaboration between mask calculation and decomposition algorithms. Based on the power n determined in each iteration, this invention generates a mask Mask = (~(2^n - 1)) & (2^W - 1). The lower n bits of this mask are irrelevant, and the higher (Wn) bits are exact matching bits. This mask, combined with the keyword Key = cur, precisely covers 2^n consecutive values in the range [cur, cur+2^n-1]. The mask calculation method and the greedy decomposition algorithm form a complete mathematical closed loop, and their collaboration ensures lossless decomposition accuracy.
[0032] like Figure 1 As shown, the above is a schematic diagram of the overall process of the TCAM-based arbitrary range rule matching method formed by the present invention.
[0033] Based on the illustration, the overall process of the arbitrary range rule matching method given in this invention is as follows: Step S101: Obtain the target value range.
[0034] This step receives range matching rules from the rule configuration system, parses the numerical range field to obtain the start value (min), end value (max), and bit width parameter W of the range. This step provides the basic data for subsequent iterative decomposition.
[0035] Step S102: Iteratively decompose the interval.
[0036] This step takes the initial value min as the current starting value cur, and repeatedly performs the sub-interval generation operation as long as cur <= max. Each iteration, based on the current starting value and the remaining interval length, determines an optimal power n through length and alignment constraints, generating a power-of-2 aligned sub-interval and its corresponding TCAM entry. After each iteration, cur = cur + 2^n is updated, and the iteration continues on the reduced remaining interval.
[0037] Step S103: Write to TCAM.
[0038] This step writes all TCAM entries generated in step S102 into the TCAM hardware according to a predetermined priority, thus completing the deployment of rules on the data plane.
[0039] Step S104, parallel matching.
[0040] When a range lookup is required, this step provides the input value as the lookup key to TCAM. TCAM uses its parallel comparison capability to complete the comparison of all entries within one clock cycle. If any entry matches, it is determined that the input value falls within the target range.
[0041] In the arbitrary range rule matching method flow formed accordingly, step S101 provides input parameters for step S102; step S102 provides a list of entries for step S103; and step S103 provides the matching hardware foundation for step S104. Steps S101 to S103 are executed when the rule is issued, and step S104 is executed when the data plane is searched, thus the two are decoupled in time.
[0042] Based on an understanding of the overall function and logical coordination of each step in the method of this invention, the specific implementation scheme of each step will be further explained from the perspective of technical implementation below.
[0043] As a specific example of an implementable form, step S101 in this method can be implemented by obtaining the target numerical range through the following scheme.
[0044] When the rule configuration system issues range rules, the rule format includes a field type identifier and a range value. This step is set to determine the bit width W based on the field type identifier: For the Transmission Control Protocol (TCP) port field and the User Datagram Protocol (UDP) port field, W=16; For the Internet Protocol version 4 (IP4) address field, W=32; For the Internet Protocol version 6 address field, W=128; for the Virtual LAN identifier field, W=12.
[0045] This step converts the range values into unsigned integer representations, obtaining the starting value min and the ending value max. If min is greater than max, this step automatically swaps their positions to ensure the normalization of the interval.
[0046] As a specific example of an implementable form, step S102 in this method can be implemented by the following scheme to achieve iterative decomposition of the interval.
[0047] See Figure 2 The diagram below provides a detailed flowchart of the interval iterative decomposition algorithm used in this step. Based on the diagram, this step specifically includes the following sub-steps: Sub-step S1021, Initialization. This sub-step sets the current starting value cur = min, the remaining ending value rem = max, and initializes the table entry list to be empty.
[0048] Sub-step S1022: Determine the termination condition. This sub-step checks if cur is greater than rem. If the condition is true, all remaining intervals have been covered, and this sub-step proceeds to sub-step S1028; if the condition is false, this sub-step continues with subsequent decomposition operations.
[0049] Sub-step S1023: Calculate the candidate maximum power. This sub-step calculates the remaining interval length L = rem - cur + 1, and determines the largest integer n that satisfies 2^n <= L.
[0050] As further explanation, this calculation is performed as follows: n = floor(log2(L)), which is the floor of L, rounded down to the nearest integer. This candidate power represents the maximum possible range that can be covered at the current starting value, considering only the length constraint.
[0051] Sub-step S1024: Alignment constraint verification and adjustment. This sub-step specifically determines whether cur satisfies cur mod 2^n = 0. If this condition is met, it means that the starting value cur of the sub-interval corresponding to the current candidate power n meets the TCAM single entry's power of 2 alignment requirement, and this sub-step keeps n unchanged. If this condition is not met, this sub-step executes n = n-1, repeating this sub-step until cur mod 2^n = 0.
[0052] As further explanation, the adjustment mechanism in this step is specifically a dynamic power-decreasing mechanism: the alignment requirement for a single TCAM entry is that the initial value must be an integer multiple of 2^n. When 2^n corresponding to the candidate power n cannot divide cur, the entry generated directly using n will not accurately cover the continuous interval starting with cur. By decreasing n, this sub-step reduces the length of the sub-interval (2^n becomes a smaller power of 2), making the alignment condition easier to satisfy. Since cur mod 1 = 0 always holds true when n = 0, this adjustment process will definitely terminate within a finite number of steps.
[0053] Sub-step S1025 generates a TCAM entry. This sub-step generates one TCAM entry based on the determined n, which includes a key and a mask: Key = cur (represented in W-bit binary); Mask = (~(2^n - 1)) & (2^W - 1).
[0054] In the binary representation of this Mask, the lower n bits are 0 (ignored bits), and the higher (Wn) bits are 1 (intended bits). For the input value Input, the matching condition is (Input XOR Key) & Mask = 0, meaning Input matches Key in all in-interest bits, and can be arbitrary in the in-interest bits. Therefore, this entry precisely covers the set of values {cur, cur+1,..., cur+2^n-1}, a total of 2^n consecutive integers.
[0055] Sub-step S1026: Record table entries. This sub-step stores the generated Key-Mask pairs into the table entry list and synchronously records the sub-range information (including the starting value, length 2^n, and ending value cur+2^n-1) to provide a basis for subsequent priority allocation.
[0056] Sub-step S1027 advances the current starting value. This sub-step sets cur = cur + 2^n, advances the current starting value to the next position in the covered interval, and then returns to sub-step S1022 to continue decomposing on the reduced remaining interval [cur, rem].
[0057] Sub-step S1028, End. This sub-step outputs a complete list of entries for subsequent writing to TCAM.
[0058] As a specific example of an implementable form, step S103 in this method can be implemented by writing to the TCAM through the following scheme.
[0059] In this step, each Key-Mask pair from the entry list is written into the available entries of TCAM in the order they were generated. For other matching fields included in the rule (such as protocol type, source IP address, etc.), this step fills in the corresponding value in the corresponding position of the Key and sets it to 1 in the corresponding position of the Mask for exact matching, ensuring that range matching and other matching conditions together constitute a complete rule matching logic.
[0060] As a specific example of an implementable form, step S104 in this method can be implemented through the following scheme to achieve parallel matching.
[0061] When a data packet arrives, this step extracts the value of the field to be searched from the data packet as the search key Input. The TCAM hardware then performs a parallel comparison of Input with all stored table entries simultaneously.
[0062] Specifically, the comparison logic in this step is as follows: For each entry, if (Input XOR Key) & Mask = 0, then the entry matches. The TCAM priority encoder selects the entry with the highest priority from all matching entries and outputs the storage address or matching flag of that entry. If at least one entry matches, this step determines that the input value falls within the target value range; if no entries match, this step determines that the input value does not fall within the target value range.
[0063] This invention provides a TCAM-based arbitrary range rule matching method. In practical applications, a corresponding software program can be constructed based on this technical solution to form a corresponding TCAM-based arbitrary range rule matching device. When the device software program runs, it executes the aforementioned TCAM-based arbitrary range rule matching method steps, realizing the function of these steps, and stores the results in a corresponding storage medium for processor retrieval and execution.
[0064] like Figure 3 As shown, the arbitrary range rule matching device based on TCAM provided by the present invention mainly includes four functional modules: interval acquisition module 310, iterative decomposition module 320, table entry writing module 330, and parallel matching module 340.
[0065] The interval acquisition module 310 in the device is configured to receive and parse range rules from the upper control plane. This module extracts the start value min, end value max, and bit width parameter W of the target numerical interval. This module is the first entry point for data into the device, and it is responsible for converting the rule-formatted interval description into structured numerical parameters required for subsequent decomposition.
[0066] The iterative decomposition module 320 in the device is communicatively connected to the interval acquisition module 310. This module is configured to receive the min, max and W output by the interval acquisition module 310. This module performs greedy iterative decomposition on the target numerical interval to generate a set of power-aligned sub-intervals of 2 and their corresponding TCAM entries, i.e., Key-Mask pairs.
[0067] The table entry writing module 330 in the device is communicatively connected to the iterative decomposition module 320. This module is configured to receive the table entry list output by the iterative decomposition module 320. This module writes each Key-Mask pair into the specified entries of the TCAM hardware through the standard bus protocol and configures the corresponding priority register.
[0068] The parallel matching module 340 in the device is connected to the TCAM hardware for communication. This module is configured to receive input values when searching the data plane. The module formats the input values into W-bit search keywords and provides them to TCAM for parallel searching. The module receives the matching results output by TCAM, determines whether the input values fall within the target range, and outputs the determination results for subsequent processing.
[0069] In the TCAM-based arbitrary range rule matching device formed accordingly, the interval acquisition module 310 provides input to the iterative decomposition module 320; the iterative decomposition module 320 provides an entry list to the entry writing module 330; after the entry writing module 330 completes the TCAM configuration, the parallel matching module 340 performs a search using the configured TCAM. The interval acquisition module 310, the iterative decomposition module 320, and the entry writing module 330 work sequentially when rules are issued, while the parallel matching module 340 works independently on the data plane.
[0070] Similarly, based on an understanding of the overall functions and logical relationships of the modules in the above-mentioned device, the specific configuration schemes of each component module will be further explained from a technical implementation perspective below.
[0071] As a specific example of an implementable form, the interval acquisition module 310 in this device, in its specific implementation, includes a rule parsing unit 311 and a parameter extraction unit 312.
[0072] Furthermore, the rule parsing unit 311 is configured to receive range rules (such as access control list rules, quality of service rules, etc.) from the upper control plane. The rule parsing unit parses the rule format and identifies the range field in it.
[0073] The parameter extraction unit 312 is configured to determine the bit width W based on the field type identifier, and convert the range value (which may be represented in decimal, dotted decimal, or hexadecimal format) into a W-bit unsigned integer to obtain the starting value min and the ending value max. If min is greater than max, the parameter extraction unit 312 automatically swaps their positions.
[0074] As a specific example of an implementable form, the iterative decomposition module 320 in this device, when specifically implemented, corresponds to step S102 in the above method scheme.
[0075] Specifically, the iterative decomposition module 320 includes an initialization subunit 321, a termination judgment subunit 322, a power calculation subunit 323, an alignment verification subunit 324, an entry generation subunit 325, a recording subunit 326, a progress subunit 327, and an output subunit 328.
[0076] Combination Figure 2 As shown, the initialization subunit 321 in this module is configured to set the current starting value cur = min, the remaining ending value rem = max, and initialize the list of entries.
[0077] The termination judgment subunit 322 in this module is configured to determine whether cur is greater than rem. If the judgment is true, the output subunit 328 is triggered. The exponentiation calculation subunit 323 in this module is configured to calculate L = rem - cur + 1, and determine the largest integer n that satisfies 2^n <= L by n = floor(log2(L)); The alignment check subunit 324 in this module is configured to determine whether cur mod 2^n = 0 is true. If the condition is not true, n is decremented until the alignment condition is met. The table entry generation subunit 325 in this module is configured to generate Key-Mask pairs based on a determined n: Key = cur, Mask = (~(2^n - 1)) & (2^W - 1); The recording subunit 326 in this module is configured to store the generated Key-Mask pairs into the table entry list and record the sub-range information; The propulsion subunit 327 in this module is configured to set cur = cur + 2^n and trigger the termination judgment subunit 322; The output subunit 328 in this module is configured to output a complete list of entries when cur is greater than rem.
[0078] In the iterative decomposition module 320 formed accordingly, each sub-unit triggers and transmits data sequentially through internal data paths. After the initialization sub-unit 321 is started, the termination judgment sub-unit 322, the exponentiation calculation sub-unit 323, the alignment verification sub-unit 324, the entry generation sub-unit 325, the recording sub-unit 326, and the advancement sub-unit 327 are executed in a loop until the termination condition is met, at which point the output sub-unit 328 outputs the result.
[0079] As a specific example of an implementable form, the table entry writing module 330 in this device, in its specific implementation, includes a priority allocation unit 331 and a TCAM interface unit 332.
[0080] Furthermore, this priority allocation unit 331 is configured to assign priority to each entry in the entry list. In a preferred embodiment, the priority allocation unit 331 allocates priorities in ascending order of sub-interval length; the smaller the sub-interval length, i.e., the smaller n, the higher the assigned priority.
[0081] TCAM interface unit 332 is configured to communicate with TCAM hardware via standard bus protocols (such as Quick Peripheral Interconnect Bus, Advanced Extensible Interface, etc.). The TCAM interface unit writes Key-Mask pairs to specified entries in the TCAM and configures the corresponding priority registers.
[0082] In one embodiment of the present invention, in order to handle scenarios where there may be overlap or inclusion relationships between range rules, the priority allocation unit 331 of the table entry writing module 330 adopts a dynamic priority allocation strategy. Specifically, this strategy allocates priorities in ascending order of sub-interval length, that is, the smaller the length of the sub-interval (corresponding to the smaller n value), the higher the TCAM priority.
[0083] Taking firewall rules as an example, suppose there are two range rules: rule A covers the range [100, 200] (this rule corresponds to the action "allow"), and rule B covers the range [150, 160] (this rule corresponds to the action "deny"), where B is a sub-range of A. According to this implementation, the length of each sub-range generated after decomposing rule B is less than the length of the sub-range generated after decomposing rule A. Therefore, all entries in B have a higher priority than entries in A. When the input value is 155, although this value falls within the coverage range of both A and B, the TCAM prioritizes matching entries in B, and the TCAM executes the "deny" action. This is completely consistent with the "sub-rule priority" semantics expected by the rule designer.
[0084] This priority allocation strategy ensures the determinism of matching results when rules are contained or overlap, and avoids mismatches caused by improper priority allocation. Those skilled in the art should understand that this priority allocation strategy is only one implementation method. In other embodiments, other priority allocation rules (such as descending order by sub-interval length, or order of rule issuance, etc.) may be adopted according to specific application requirements, and these alternative methods are all within the scope of this invention.
[0085] As a specific example of an implementable form, the parallel matching module 340 in this device, in its specific implementation, includes a keyword generation unit 341 and a matching result determination unit 342.
[0086] Furthermore, the search keyword generation unit 341 in this module is configured to extract the value of the field to be searched from the input data packet. The search keyword generation unit formats the extracted value into a W-bit search keyword and provides it to TCAM as search input.
[0087] Furthermore, the matching result determination unit 342 in this module is configured to receive the matching flag (which indicates whether a matching entry exists) and the matching entry address (which indicates the specific entry that is matched) output by the TCAM. The matching result determination unit outputs a determination result based on whether a matching entry exists: if a matching entry exists, it outputs "Matched" and carries the information of the matched entry; if no matching entry exists, it outputs "Not Matched".
[0088] As a further optimization of the basic decomposition algorithm described above, in another embodiment of the present invention, after completing all iterations in step S102, the iterative decomposition module 320 adds a filler merging optimization step. This optimization step is applicable to extreme interval scenarios—when the length of the target interval is close to 2^W, and the basic decomposition generates multiple single-valued sub-intervals of length 1 (i.e., n=0) near the interval endpoints, this optimization step attempts to merge several adjacent single-valued sub-intervals into a larger entry, thereby further reducing the total number of entries.
[0089] Specifically, the iterative decomposition module 320 checks several sub-intervals at the end of the entry list. If there are two or more consecutive sub-intervals with a length of 1 (n=0), and these sub-intervals are numerically consecutive (i.e., the starting value of the next sub-interval is equal to the ending value of the previous sub-interval plus 1), then the iterative decomposition module 320 attempts to merge these consecutive single-value sub-intervals. During the merging process, the iterative decomposition module takes the starting value of the merged new interval as the starting value of the first sub-interval and the ending value as the ending value of the last sub-interval. Then, the iterative decomposition module re-executes the alignment constraint check on the merged interval to determine whether there exists an n (n >= 1) such that the merged interval is exactly a power-of-2 aligned interval. If it exists, the iterative decomposition module replaces the original multiple single-value entries with a new entry.
[0090] For example, for the extreme interval [1, 65534] where W=16, the basic decomposition produces two adjacent single-valued sub-intervals [65534, 65534] and [65535, 65535] at the end. Through complement merging optimization, the iterative decomposition module 320 merges [65534, 65535] into an interval of length 2, and verifies whether 65534 is an integer multiple of 2 (65534 mod 2 = 0 holds true, n=1 satisfies the alignment condition). Thus, the iterative decomposition module replaces the original two single-valued entries with a single entry with Key=65534 and Mask=0xFFFE. The final total number of entries is reduced from 30 to 29.
[0091] This fill-in and merge optimization strategy has significant practical value in application scenarios with extremely limited table entry resources (such as backbone routers that need to store thousands of range rules simultaneously). Therefore, based on this optimization scheme, table entry storage efficiency can be improved in specific scenarios.
[0092] It should be noted that this optimization step is optional and is not necessary to implement the basic range matching function of this invention. Without performing this optimization step, the basic algorithm can still work normally and reach the upper bound of 2W-2 for the number of entries.
[0093] As a further explanation, the TCAM-based arbitrary range rule matching device formed based on the aforementioned scheme is also configured to execute the following operational logic: Rule configuration phase: The upper control plane sends range rules to the interval acquisition module 310. The rule parsing unit 311 of the interval acquisition module 310 parses the rule format and identifies the range field. The parameter extraction unit 312 determines W according to the field type and converts the range value into min and max. The interval acquisition module 310 passes min, max, and W to the iterative decomposition module 320. The initialization subunit 321 of the iterative decomposition module 320 sets cur=min and rem=max. Then, the termination judgment subunit 322, the exponentiation calculation subunit 323, the alignment verification subunit 324, the entry generation subunit 325, the recording subunit 326, and the advancement subunit 327 are executed in sequence until cur is greater than rem. The output subunit 328 outputs a complete list of Key-Mask entries. The priority allocation unit 331 of the entry writing module 330 assigns priority to each entry, and the TCAM interface unit 332 writes the entries to the TCAM hardware, completing the rule configuration.
[0094] Data lookup phase: When a data packet arrives, the lookup keyword generation unit 341 of the parallel matching module 340 extracts the value of the field to be searched from the data packet. This lookup keyword generation unit generates a W-bit lookup keyword and provides it to the TCAM. The TCAM compares all entries in parallel within one clock cycle, and outputs a matching flag and the address of the matching entry. The matching result determination unit 342 determines whether the input value falls within the target range based on the TCAM's output and outputs the determination result for subsequent access control list action execution, quality of service scheduling, and other processing.
[0095] The following specific application examples further illustrate the application and implementation process of the present invention and the technical effects that can be obtained.
[0096] Example 1: Matching TCP port range [6881, 6889] Using a network firewall access control list policy as an example, the goal is to allow traffic destined for ports within the range [6881, 6889].
[0097] (1) System composition and deployment: The system in this embodiment includes: TCAM hardware deployed in a network processor, and an interval acquisition module 310 and an iterative decomposition module 320 (implemented in software) running on a control plane processor. The entry writing module 330 is connected to the TCAM hardware via a quick peripheral interconnect bus.
[0098] (2) Rule reception and parameter extraction: The policy system issues a five-tuple rule with the destination port field min=6881 and max=6889. The interval acquisition module 310 determines W=16 based on the field type (port), and this interval acquisition module represents min and max as 16-bit unsigned integers.
[0099] (3) Iterative decomposition process: like Figure 5 The iterative decomposition module 320 performs iterative decomposition on [6881, 6889]: First iteration: L = 6889 - 6881 + 1 = 9, n = floor(log2(9)) = 3. Alignment check: 6881 mod 2^3 = 6881 mod 8 = 1 ≠ 0, decrease n to 2; 6881 mod 4 = 1 ≠ 0, decrease n to 1; 6881 mod 2 = 1 ≠ 0, decrease n to 0; 6881 mod 1 = 0, n=0. Entry generation: Key=6881, Mask = (~(1-1)) & (2^16-1) = 0xFFFF, covering [6881, 6881].
[0100] Second iteration: cur=6882, L=6889-6882+1=8, n=3. 6882 mod 8 = 2 ≠ 0, decrease n to 2; 6882 mod 4 = 2 ≠ 0, decrease n to 1; 6882 mod 2 = 0, n=1. Entry generation: Key=6882, Mask =(~(2-1)) & 0xFFFF = 0xFFFE, covering [6882, 6883].
[0101] Third iteration: cur=6884, L=6889-6884+1=6, n=2. 6884 mod 4 = 0, n=2. Entry generation: Key=6884, Mask = (~(4-1)) & 0xFFFF = 0xFFFC, covering [6884, 6887].
[0102] 4th iteration: cur=6888, L=6889-6888+1=2, n=1. 6888 mod 2 = 0, n=1. Entry generation: Key=6888, Mask = (~(2-1)) & 0xFFFF = 0xFFFE, covering [6888, 6889].
[0103] Fifth iteration: cur=6890 is greater than max=6889, so the iteration terminates.
[0104] A total of 4 entries were generated, accurately covering 9 port values, with no omissions or out-of-bounds errors.
[0105] (4) Table entry writing and matching: The entry writing module 330 writes four Key-Masks into the TCAM, prioritizing them in ascending order of sub-interval length (smaller length, higher priority). When a message arrives, the parallel matching module 340 extracts the destination port field as the lookup keyword. If the destination port is 6885, then the third entry in the TCAM matches (6885 ∈ [6884, 6887]), and the TCAM determines that the port rule has been matched. The entire lookup process is completed within a single clock cycle.
[0106] In this embodiment, the original interval [6881, 6889] is an unaligned interval (the starting value 6881 is not an integer multiple of 2 greater than 1). Using a hash-based exact matching scheme requires 9 entries, while using a prefix extension scheme requires approximately 8 entries. This invention requires only 4 entries, achieving an entry compression rate of 56%. Furthermore, this invention requires no additional hardware, and the matching latency is the same as a single exact matching rule.
[0107] Example 2: Matching IPv4 address ranges [192.168.1.100, 192.168.1.200] Taking a routing policy scenario as an example, the requirement is to match data packets whose source IP address falls within the range of [192.168.1.100, 192.168.1.200].
[0108] (1) System deployment: In this embodiment, the TCAM is deployed in the forwarding engine of the core router, with a bit width W=32.
[0109] (2) Rule reception and parameter extraction: The interval acquisition module 310 converts 192.168.1.100 into a 32-bit unsigned integer min=3232235876, and the interval acquisition module converts 192.168.1.200 into max=3232235976.
[0110] (3) Iterative decomposition process: like Figure 4 The iterative decomposition module 320 performs iterative decomposition on the interval, generating the following sub-intervals and corresponding table entries in sequence: [3232235876, 3232235879] (n=2): Key=3232235876, the lower 2 bits of the Mask are ignored bits, covering 192.168.1.100 ~ 192.168.1.103; [3232235880, 3232235887] (n=3): covers 192.168.1.104 ~ 192.168.1.111; [3232235888, 3232235903] (n=4): covers 192.168.1.112 ~ 192.168.1.127; [3232235904, 3232235967] (n=6): covers 192.168.1.128 ~ 192.168.1.191; [3232235968, 3232235975] (n=3): covers 192.168.1.192 ~ 192.168.1.199; [3232235976, 3232235976] (n=0): Covers 192.168.1.200.
[0111] There are 6 entries in total, precisely covering 101 IPv4 addresses.
[0112] (4) Matching verification: When the source IP is 192.168.1.150 (decimal 3232235934), the value falls into the fourth sub-range [3232235904, 3232235967], and the corresponding entry matches, so the rule is hit.
[0113] Compared to the traditional scheme using classless inter-domain routing prefix aggregation, this embodiment requires splitting the interval into multiple non-contiguous prefixes, resulting in approximately 8 to 10 entries. Furthermore, this scheme is only applicable to IP address fields. This invention requires only 6 entries, and the same algorithm logic can be directly applied to other field types such as ports and virtual LAN identifiers, demonstrating the field versatility advantage of this invention.
[0114] Example 3: Verification and Comparison of the Upper Bound of the Number of Table Items For any interval [min, max] of a W-bit field, the algorithm of this invention generates at most 2W-2 entries. Taking the extreme interval [1, 65534] when W=16 as an example: In the first iteration: cur=1, L=65534, n=15, but 1 mod 32768 ≠ 0, so the value is decreased to n=0, generating [1,1]. Subsequent iterations generate [2,3], [4,7], [8,15], ..., [32768, 65535] (finally truncated to 65534). Approximately 30 entries are generated in total.
[0115] Compared with existing technologies: hash exact matching requires 65,534 entries; prefix expansion requires 30 entries, but this prefix expansion uses a static enumeration method; although the fence coding can reduce the expansion factor to 1, this fence coding requires an additional W TCAM bits; the present invention achieves the upper bound of 30 entries without adding any additional TCAM bits, and the present invention uses a dynamic greedy iterative decomposition path.
[0116] As a supplementary explanation, in relation to the arbitrary range rule matching scheme based on TCAM provided by the present invention, the embodiments of the present invention also provide a computer-readable storage medium storing a program thereon, which, when executed by a processor, implements the steps of the above-described arbitrary range rule matching method.
[0117] This invention also provides a processor for running a program, wherein the program executes the steps of the above-described arbitrary range rule matching method during runtime.
[0118] This invention also provides a terminal device, which includes a processor, a memory, and a program stored in the memory and executable on the processor. The program code is loaded and executed by the processor to implement the steps of the above-described arbitrary range rule matching method.
[0119] The present invention also provides a computer program product, which, when executed on a data processing device, is adapted to perform the steps of the above-described arbitrary range rule matching method.
[0120] In the above embodiments, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions in other embodiments.
[0121] Finally, it should be noted that those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and modules described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0122] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations and / or block diagrams. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0123] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0124] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0125] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0126] Memory may include non-persistent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM. Memory is an example of computer-readable media.
[0127] Computer-readable media, including both permanent and non-permanent, removable and non-removable media, can store information using any method or technology. Information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transfer medium that can be used to store information accessible by a computing device.
[0128] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.
[0129] The foregoing has shown and described the basic principles, main features, and advantages of the present invention. Those skilled in the art should understand that the present invention is not limited to the above embodiments. The embodiments and descriptions in the specification are merely illustrative of the principles of the invention. Various changes and modifications can be made to the invention without departing from its spirit and scope, and all such changes and modifications fall within the scope of the present invention as claimed. The scope of protection of this invention is defined by the appended claims and their equivalents.
Claims
1. A method for arbitrary range rule matching based on TCAM, characterized in that, The method includes: Obtain the target numerical range to be matched, wherein the target numerical range has a start value, an end value, and a bit width parameter; Using the initial value as the current starting value, iteratively perform the sub-interval generation operation until the current starting value is greater than the ending value; The sub-interval generation operation includes: determining a candidate power based on the current starting value and the ending value, wherein the candidate power is the largest integer n satisfying that 2^n does not exceed the remaining length of the target value interval; verifying whether the current starting value satisfies the alignment condition cur mod 2^n = 0; if the current starting value does not satisfy the alignment condition, decreasing the candidate power until the current starting value satisfies the alignment condition; generating a TCAM entry based on the current starting value and the candidate power satisfying the alignment condition, wherein the entry contains a key and a mask, the key being equal to the current starting value, and the mask being determined based on the candidate power and the bit width parameter; and updating the current starting value to cur = cur + 2^n. Write each generated TCAM entry into the TCAM.
2. The method according to claim 1, characterized in that, The mask is determined as follows: the bitwise inverse of 2^n - 1 is calculated, and then the bitwise inverse is ANDed with 2^W - 1, where W is the bit width parameter.
3. The method according to claim 1, characterized in that, The method further includes: after writing to the TCAM, in response to receiving the input value to be searched, using the input value as a search keyword and comparing it in parallel with each entry in the TCAM; if at least one entry matches, then determining that the input value falls within the target value range.
4. The method according to claim 1, characterized in that, The field type of the target numerical range can be any of the following: Transmission Control Protocol port field, User Datagram Protocol port field, Internet Protocol version 4 address field, Internet Protocol version 6 address field, or Virtual Local Area Network identifier field.
5. The method according to claim 1, characterized in that, The bit width parameter is determined based on the field type of the target numerical range, and the method uses the same iterative decomposition logic for any field type.
6. A TCAM-based arbitrary range rule matching device, characterized in that, The device includes: The interval acquisition module is configured to acquire the target numerical interval to be matched, wherein the target numerical interval has a start value, an end value, and a bit width parameter; The iterative decomposition module is configured to iteratively perform a sub-interval generation operation with the initial value as the current starting value until the current starting value is greater than the ending value. The sub-interval generation operation includes: determining a candidate power based on the current starting value and the ending value, wherein the candidate power is the largest integer n that satisfies 2^n not exceeding the remaining length of the target value interval; verifying whether the current starting value satisfies the alignment condition cur mod 2^n = 0; if the current starting value does not satisfy the alignment condition, decreasing the candidate power until the current starting value satisfies the alignment condition; generating a TCAM entry based on the current starting value and the candidate power that satisfies the alignment condition, wherein the entry contains a key and a mask, the key being equal to the current starting value, and the mask being determined based on the candidate power and the bit width parameter; and updating the current starting value to cur = cur + 2^n. The table entry writing module is configured to write each generated TCAM table entry into the TCAM.
7. The apparatus according to claim 6, characterized in that, The iterative decomposition module determines the mask in the following manner: it calculates the bitwise inversion of 2^n - 1, and then performs a bitwise AND operation between the bitwise inversion result and 2^W - 1, where W is the bit width parameter.
8. The apparatus according to claim 6, characterized in that, The device further includes: The parallel matching module is configured to, in response to receiving an input value to be searched, use the input value as a search keyword and compare it in parallel with each entry in the TCAM, and determine that the input value falls within the target value range when at least one entry matches.
9. The apparatus according to claim 6, characterized in that, The field type of the target numerical range can be any of the following: Transmission Control Protocol port field, User Datagram Protocol port field, Internet Protocol version 4 address field, Internet Protocol version 6 address field, or Virtual Local Area Network identifier field.
10. The apparatus according to claim 6, characterized in that, The bit width parameter is determined according to the field type of the target value range, and the iterative decomposition module uses the same iterative decomposition logic for any field type.
Citation Information
Patent Citations
TCAM-Based Range Multi-Matching Two-Level Hierarchical Search Method
CN103546378B
TCAM with efficient range search capability
US20140268971A1
Method and apparatus for multi-range look-up
US9299433B1
