Rule storage method, message processing method, device, electronic device and medium
By converting ACL rule data into a check block and storing it in RAM, the storage space occupation problem caused by the increase in the scale of ACL rule is solved, and storage and access efficiency is improved.
Patent Information
- Application Number
- CN202211336843.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-10-28
- Publication Date
- 2024-11-19
- Estimated Expiration
- 2042-10-28
AI Technical Summary
As the scale of rules in the access control list (ACL) increases, the storage space occupied by random access memory (RAM) gradually increases, resulting in insufficiency of storage and high bandwidth.
Convert the rule data to be stored into a check block, including a check block of mask type and a non-mask type. The check block of non-mask type does not store the mask, and only the key values of the effective bits are stored, so as to Reduce RAM storage space usage.
It effectively reduces the storage space of RAM, improves storage efficiency and access bandwidth, and reduces the overhead of search efficiency.
Smart Images

Figure CN115865843B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication technology, and in particular to a rule storage method, a message processing method, a device, an electronic device and a medium. Background Art
[0002] Access Control Lists (ACLs) are widely used in electronic devices such as routers and switches. ACLs include a set of rules for identifying messages. Rules are judgment statements used to describe message matching conditions. For example, matching conditions can be the source address, destination address, and port number of a message. Electronic devices can match received messages according to the rules in the ACL and process the matched messages according to pre-set policies. Electronic devices can use ACLs to implement services such as message filtering, routing, network security, and Quality of Service (QoS).
[0003] Currently, the rules in ACL can be stored in Random Access Memory (RAM). The matching domain of each rule can be converted into two parts: key (key value) and mask (mask). The matching domain of each rule is stored in the form of key and mask. The key represents the specific value of each domain in the rule. The key can be called a prefix. The mask represents which values in the key need to be matched, that is, it is used to indicate the effective bit in the key. As the scale of the rules included in the ACL continues to increase, a large amount of RAM storage space will be consumed. Summary of the invention
[0004] The purpose of the embodiments of the present invention is to provide a rule storage method, a message processing method, an apparatus, an electronic device and a medium to reduce the occupation of RAM storage space. The specific technical solution is as follows:
[0005] In a first aspect, the present application provides a rule storage method, the method being applied to an electronic device, wherein the electronic device is provided with a RAM, the method comprising:
[0006] Acquire a rule to be stored, wherein the rule to be stored includes a matching domain, wherein the matching domain includes target rule data, and the target rule data is a key value and a mask in the matching domain;
[0007] converting the target rule data into at least one check block, and storing the target rule data in the RAM in the form of the check block;
[0008] Among them, the types of the check blocks include mask type and non-mask type, the rule data in the check blocks of the mask type include key values and masks, and the rule data in the check blocks of the non-mask type include key values with continuous valid bits, and the valid bits are bits with mask values of 1.
[0009] In a possible implementation manner, converting the target rule data into at least one check block includes:
[0010] Taking the first valid bit of the target rule data as the current starting bit, based on the encoding conditions satisfied by the rule data of a first number of consecutive bits starting from the current starting bit, converting the rule data of a first number of consecutive bits into a check block; the first number is less than or equal to the CPU bit width of the electronic device and greater than a preset number, and the preset number is the number of bits used to store the key value in the check block of the mask type;
[0011] The first valid bit is searched from the bits in the target rule data that have not been converted into the check block, the found valid bit is used as the current starting bit, the encoding condition satisfied by the rule data based on the first number of consecutive bits starting from the current starting bit is returned, and the step of converting the first number of consecutive bits of rule data into a check block is performed until all the rule data with valid bits included in the target rule data are converted into check blocks.
[0012] In a possible implementation, based on the encoding condition satisfied by the first number of consecutive regular data bits starting from the current start bit, converting the first number of consecutive regular data bits into a check block includes:
[0013] Determining whether the first number of consecutive bits of regular data meet a non-mask type encoding condition;
[0014] If satisfied, converting the key value in the first number of consecutive bits of rule data into the non-mask type check block;
[0015] If not, the first number is determined to be the preset number, and the key value and mask in the rule data of the continuous first number of bits are converted into a check block of the mask type.
[0016] In a possible implementation, the non-mask type includes a full data type, a first length type, and a second length type;
[0017] The encoding conditions of the full data type include: the current starting bit is the first bit of a byte included in the target rule data; and in the mask of the target rule data, the values of consecutive CPU bit width bits starting from the current starting bit are all 1, and the first number is the CPU bit width;
[0018] The encoding condition of the first length type includes: the current start bit is the first bit of a byte included in the target rule data; and in the mask of the target rule data, the values of the first number of bits starting from the current start bit are all 1, and the first number is less than the difference between the CPU bit width and the number of bits occupied by the check block type field;
[0019] The encoding condition of the second length type includes: the current start bit is not the first bit of a byte included in the target rule data; and in the mask of the target rule data, the values of the first number of bits starting from the current start bit are all 1, and the first number is less than the difference between the CPU bit width and the number of bits occupied by the check block type field and the number of bits occupied by the start bit field;
[0020] The determining whether the first number of consecutive regular data bits meets a non-mask type encoding condition includes:
[0021] If the regular data of the first number of consecutive bits meets any one of the encoding conditions of the full data type, the first length type and the second length type, it is determined that the encoding condition of the non-mask type is met; otherwise, it is determined that the encoding condition of the non-mask type is not met.
[0022] In a possible implementation, converting the key value in the first number of consecutive bits of regular data into the non-mask type check block includes:
[0023] If the first number of consecutive digits of regular data meet the encoding condition of the full data type, converting the key value in the first number of consecutive digits of regular data into a check block of the full data type;
[0024] If the first number of consecutive regular data bits meet the encoding condition of the first length type and do not meet the encoding condition of the full data type, converting the key value in the first number of consecutive regular data bits into a check block of the first length type;
[0025] If the first number of consecutive regular data bits meet the encoding condition of the second length type, the first number of consecutive regular data bits are converted into a check block of the second length type.
[0026] In a possible implementation, the check block of all data types is used to store the key value in the rule data of the continuous first number of bits;
[0027] The lowest three bits of the check block of the first length type are used to store the type code of the first length type, and the bits from the highest bit to the fourth to last bit are used to store: the key value in the rule data of the first number of bits and a 0, and the value of the unoccupied bit is set to 1 by default;
[0028] The highest three bits in the check block of the second length type are used to store the offset of the current start bit within the byte, the lowest three bits are used to store the type code of the second length type, and the remaining bits are used to store in sequence: the key value in the rule data of the continuous first number of bits and a 0, and the unoccupied bits are set to 1 by default;
[0029] The highest three bits in the check block of the mask type are used to store the offset of the current starting bit within the byte, the lowest three bits are used to store the type code of the mask type, and the remaining bits are used to store in sequence: the key value and mask in the rule data of the continuous first number of bits.
[0030] In a possible implementation, the method further includes:
[0031] If the target rule data includes range-type rule data, and the maximum and minimum values of the range-type rule data are both less than 2 13 , then converting the rule data of the range type into a check block of the first range type;
[0032] If the minimum value of the rule data of the range type is greater than or equal to 2 13 , or the maximum value is greater than or equal to 2 13 , then converting the rule data of the range type into a check block of a second range type and a check block of a third range type;
[0033] The highest 13 bits of the check block of the first range type are used to store the maximum value, the following 13 bits are used to store the minimum value, the following 2 bits are used to store the first subtype code, and the lowest 3 bits are used to store the code of the range type, and the first subtype code is used to indicate that the check block includes a matching range;
[0034] The highest 26 bits of the second range type check block are used to store the minimum value, the following two bits are used to store the second subtype code, and the lowest 3 bits are used to store the code of the range type, and the second subtype code is used to indicate that the check block includes a minimum value of a matching range;
[0035] The highest 26 bits of the third range type check block are used to store the maximum value, the following two bits are used to store the third subtype code, and the lowest 3 bits are used to store the range type code, and the third subtype code is used to indicate that the check block includes a maximum value of a matching range.
[0036] In a possible implementation, each check block stored in the RAM includes a byte offset field before it, and the byte offset field before each check block is used to indicate the byte offset of the start bit of the rule data in the check block in the target rule data.
[0037] In a possible implementation, the obtaining of the rules to be stored includes:
[0038] Acquire a global rule set and common rules of a plurality of rules included in the global rule set;
[0039] Based on the common rules, pending rules corresponding to each rule included in the global rule set are generated, and each pending rule is used as the rule to be stored; for each bit, if a rule has the same value in the bit as the common rule corresponding to the rule, then the value of the pending rule corresponding to the rule in the bit is a wildcard, otherwise the value of the pending rule corresponding to the rule in the bit is the same as the value of the rule in the bit.
[0040] In a second aspect, the present application provides a message processing method, which is applied to an electronic device, wherein a random access memory RAM is provided in the electronic device, and the target rule data in the matching domain of each rule in the specified rule table of the RAM is stored in the form of a check block, and the types of the check blocks include mask types and non-mask types. The rule data in the check block of the mask type includes a key value and a mask, and the rule data in the check block of the non-mask type includes a key value with a continuous effective bit, and the effective bit is a bit with a mask value of 1; characterized in that the method includes:
[0041] Extract and search key values from the message to be processed;
[0042] Determine the to-be-matched rule in the specified rule table based on the search key value;
[0043] For each check block included in the to-be-matched rule, determining the length of the to-be-matched data according to the type of the check block;
[0044] According to the starting position of the rule data included in the check block, starting from the same starting position in the search key value, extracting the data to be matched according to the length of the data to be matched, and matching the data to be matched with the rule data included in the check block;
[0045] If the search key value successfully matches all the check blocks in the to-be-matched rule, it is determined that the search key value successfully matches the to-be-matched rule;
[0046] Based on the matching result between the search key value and the rule to be matched, the message to be processed is processed.
[0047] In a possible implementation, the RAM further stores a linear table, the linear table includes a plurality of base addresses, and the base addresses are used to locate the block rule table; and determining the to-be-matched rule in the specified rule table based on the search key value includes:
[0048] Using bits within a preset range in the search key value as a search index, and determining a target base address pointed to by the search index in the linear table;
[0049] Locating a block rule table based on the target base address, and using the located block rule table as the designated rule table;
[0050] The rules included in the specified rule table are used as rules to be matched.
[0051] In a possible implementation, the electronic device is further provided with a ternary content addressable memory TCAM, the TCAM stores a common rule table, and the RAM stores a main rule table and a block rule table:
[0052] The block rule table stores a plurality of rules having the same common rule;
[0053] The main rule table includes at least two fields: a split position set and a base address. The rules in the block rule table belong to a subtree determined by the split position set. The split position set is used to locate the position of the rule corresponding to the key value in the block rule table. The base address is used to locate the block rule table. The value of the non-wildcard bit in the common rule is the same as the value of the same bit of all rules in the block rule table pointed to by the base address.
[0054] The common rule table stores common rules, and the common rule table has the same index as the main rule table corresponding to the same block rule table;
[0055] The determining the to-be-matched rule in the specified rule table based on the search key value includes:
[0056] Using the search key value to perform a match in the common rule table, obtaining a primary index of a target entry in the main rule table;
[0057] Locate a block rule table based on a base address included in the target table entry pointed to by the primary index, and use the located block rule table as the designated rule table;
[0058] Determine a secondary index of a to-be-matched rule in the specified rule table based on the search key value and a split position set included in the target table entry pointed to by the primary index;
[0059] The rule pointed to by the secondary index is used as the rule to be matched.
[0060] In a possible implementation, the common rule table, the main rule table and the block rule table are generated in the following manner:
[0061] Construct a rule decision tree based on the balance bit of each rule in the global rule set, where the balance bit is the bit with the smallest difference between the number of rules with a value of 0 and the number of rules with a value of 1;
[0062] Starting from each leaf node of the rule decision tree, traverse upward in breadth-first order to obtain the deepest rule subtree corresponding to each leaf node, wherein the deepest rule subtree is the deepest rule subtree among the rule subtrees whose number of balance bits is less than or equal to the preset splitting bit depth;
[0063] Based on each deepest rule subtree, the common rule table, the main rule table and the block rule table are generated.
[0064] In a possible implementation, the generating the common rule table, the main rule table and the block rule table based on each deepest rule subtree includes:
[0065] Based on the rule sets included in each deepest rule subtree, a block rule table is generated respectively;
[0066] Based on the balance bits included in each deepest rule subtree, a split position set is generated respectively;
[0067] Generate the main rule table based on the split position set corresponding to each deepest rule subtree and the base address of the block rule table;
[0068] The common rule table is generated based on the table entry index in the main rule table corresponding to each deepest rule subtree and the common rules of the rule set included in each deepest rule subtree.
[0069] In a possible implementation, the step of constructing a rule decision tree based on the balance position of each rule in the global rule set includes:
[0070] Using a global rule set as a rule set to be split, wherein the global rule set is included in a root node of a rule decision tree;
[0071] Determine the balance position of each rule in the rule set to be split;
[0072] Add the rule whose balance bit value is 0 in the to-be-split rule set to the first side child node, and add the rule whose balance bit value is 1 in the to-be-split rule set to the second side child node;
[0073] For each child node obtained by splitting, if the child node includes multiple rules, the rule set included in the child node is used as the rule set to be split, and the step of determining the balance position of the rules in the rule set to be split is re-executed until each child node obtained by splitting includes one rule, thereby obtaining the rule decision tree.
[0074] In a possible implementation, the processing of the message to be processed based on the matching result between the search key value and the rule to be matched includes:
[0075] If there is one rule to be matched, and the search key value successfully matches the rule to be matched, the message to be processed is processed according to the action field corresponding to the rule to be matched;
[0076] If there are multiple rules to be matched, the message to be processed is processed according to the action field corresponding to a rule to be matched that successfully matches the search key value.
[0077] In a possible implementation manner, determining the length of the data to be matched according to the type of the check block includes:
[0078] If the type of the check block is the mask type, determining that the length of the data to be matched is the length of the preset number of bits;
[0079] If the type of the check block is the full data type, determining that the length of the data to be matched is the CPU bit width of the electronic device;
[0080] If the type of the check block is the first length type or the second length type, search for the first bit whose value is 0 from the lowest bit of the rule data included in the check block toward the highest bit, and determine that the length of the data to be matched is the length from the highest bit to the bit found;
[0081] If the type of the check block is a range type, the length of the data to be matched is determined to be a fixed length of the range type field.
[0082] In a possible implementation, matching the to-be-matched data with the rule data included in the verification block includes:
[0083] If the type of the check block is the mask type, the key value in the check block and the mask are ANDed, and the result obtained after the ANDed operation is compared with the data to be matched. If they are completely consistent, the match is successful;
[0084] If the type of the check block is the full data type, the data to be matched is compared with all the rule data in the check block. If they are completely consistent, the match is successful;
[0085] If the type of the check block is the first length type or the second length type, starting from the highest bit of the rule data included in the check block, extract the rule data according to the length of the data to be matched, and compare the extracted rule data with the data to be matched. If they are completely consistent, the match is successful;
[0086] If the type of the check block is a range type, a matching range is obtained from the check block, and if the data to be matched belongs to the matching range, the match is successful.
[0087] In a third aspect, the present application provides a rule storage device, the device is applied to an electronic device, the electronic device is provided with a RAM, and the device includes:
[0088] An acquisition module, used for acquiring rules to be stored, wherein the rules to be stored include a matching domain, wherein the matching domain includes target rule data, and the target rule data is a key value and a mask in the matching domain;
[0089] A conversion module, used for converting the target rule data into at least one check block;
[0090] A storage module, used for storing the target rule data in the RAM in the form of a check block;
[0091] Among them, the types of the check blocks include mask type and non-mask type, the rule data in the check blocks of the mask type include key values and masks, and the rule data in the check blocks of the non-mask type include key values with continuous valid bits, and the valid bits are bits with mask values of 1.
[0092] In a possible implementation, the conversion module is specifically configured to:
[0093] Taking the first valid bit of the target rule data as the current starting bit, based on the encoding conditions satisfied by the rule data of a first number of consecutive bits starting from the current starting bit, converting the rule data of a first number of consecutive bits into a check block; the first number is less than or equal to the CPU bit width of the electronic device and greater than a preset number, and the preset number is the number of bits used to store the key value in the check block of the mask type;
[0094] The first valid bit is searched from the bits in the target rule data that have not been converted into the check block, the found valid bit is used as the current starting bit, the encoding condition satisfied by the rule data based on the first number of consecutive bits starting from the current starting bit is returned, and the step of converting the first number of consecutive bits of rule data into a check block is performed until all the rule data with valid bits included in the target rule data are converted into check blocks.
[0095] In a possible implementation, the conversion module is further configured to:
[0096] If the target rule data includes range-type rule data, and the maximum and minimum values of the range-type rule data are both less than 2 13 , then converting the rule data of the range type into a check block of the first range type;
[0097] If the minimum value of the rule data of the range type is greater than or equal to 2 13 , or the maximum value is greater than or equal to 2 13 , then converting the rule data of the range type into a check block of a second range type and a check block of a third range type;
[0098] The highest 13 bits of the check block of the first range type are used to store the maximum value, the following 13 bits are used to store the minimum value, the following 2 bits are used to store the first subtype code, and the lowest 3 bits are used to store the code of the range type, and the first subtype code is used to indicate that the check block includes a matching range;
[0099] The highest 26 bits of the second range type check block are used to store the minimum value, the following two bits are used to store the second subtype code, and the lowest 3 bits are used to store the code of the range type, and the second subtype code is used to indicate that the check block includes a minimum value of a matching range;
[0100] The highest 26 bits of the third range type check block are used to store the maximum value, the following two bits are used to store the third subtype code, and the lowest 3 bits are used to store the range type code, and the third subtype code is used to indicate that the check block includes a maximum value of a matching range.
[0101] In a possible implementation, each check block stored in the RAM includes a byte offset field before it, and the byte offset field before each check block is used to indicate the byte offset of the start bit of the rule data in the check block in the target rule data.
[0102] In a fourth aspect, the present application provides a message processing device, the device is applied to an electronic device, the electronic device is provided with a random access memory RAM, the target rule data in the matching domain of each rule in the specified rule table of the RAM is stored in the form of a check block, the types of the check block include a mask type and a non-mask type, the rule data in the check block of the mask type includes a key value and a mask, the rule data in the check block of the non-mask type includes a key value with a continuous effective bit, and the effective bit is a bit with a mask value of 1; characterized in that the device includes:
[0103] An extraction module is used to extract and search key values from the message to be processed;
[0104] A determination module, configured to determine the to-be-matched rule in the specified rule table based on the search key value; and for each check block included in the to-be-matched rule, determine the length of the to-be-matched data according to the type of the check block;
[0105] A matching module, configured to extract the data to be matched according to the length of the data to be matched, starting from the same starting position in the search key value according to the starting position of the rule data included in the check block, and match the data to be matched with the rule data included in the check block; if the search key value successfully matches all the check blocks in the rule to be matched, it is determined that the search key value successfully matches the rule to be matched;
[0106] A processing module is used to process the message to be processed based on the matching result between the search key value and the rule to be matched.
[0107] In a possible implementation, the RAM further stores a linear table, the linear table includes a plurality of base addresses, and the base addresses are used to locate the block rule table; the determination module is specifically used to:
[0108] Using bits within a preset range in the search key value as a search index, and determining a target base address pointed to by the search index in the linear table;
[0109] Locating a block rule table based on the target base address, and using the located block rule table as the designated rule table;
[0110] The rules included in the specified rule table are used as rules to be matched.
[0111] In a possible implementation, the electronic device is further provided with a ternary content addressable memory TCAM, the TCAM stores a common rule table, and the RAM stores a main rule table and a block rule table:
[0112] The block rule table stores a plurality of rules having the same common rule;
[0113] The main rule table includes at least two fields: a split position set and a base address. The rules in the block rule table belong to a subtree determined by the split position set. The split position set is used to locate the position of the rule corresponding to the key value in the block rule table. The base address is used to locate the block rule table. The value of the non-wildcard bit in the common rule is the same as the value of the same bit of all rules in the block rule table pointed to by the base address.
[0114] The common rule table stores common rules, and the common rule table has the same index as the main rule table corresponding to the same block rule table;
[0115] The determination module is specifically used for:
[0116] Using the search key value to perform a match in the common rule table, obtaining a primary index of a target entry in the main rule table;
[0117] Locate a block rule table based on a base address included in the target table entry pointed to by the primary index, and use the located block rule table as the designated rule table;
[0118] Determine a secondary index of a to-be-matched rule in the specified rule table based on the search key value and a split position set included in the target table entry pointed to by the primary index;
[0119] The rule pointed to by the secondary index is used as the rule to be matched.
[0120] In a fifth aspect, an embodiment of the present application provides an electronic device, including a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus;
[0121] Memory, used to store computer programs;
[0122] The processor is used to implement the method steps described in the first aspect or the second aspect when executing the program stored in the memory.
[0123] In a sixth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method steps described in the first aspect or the second aspect are implemented.
[0124] In a seventh aspect, an embodiment of the present application further provides a computer program product comprising instructions, which, when executed on a computer, enables the computer to execute the method steps described in the first aspect or the second aspect above.
[0125] By adopting the above technical solution, the electronic device can convert the target data in the matching domain of the rule to be stored into at least one check block, and store the target rule data in the form of a check block in the RAM. The types of check blocks include mask type and non-mask type. The rule data in the check block of the mask type includes a key value and a mask, and the rule data in the check block of the non-mask type includes a key value with a continuous valid bit position. It can be seen that the check block of the non-mask type does not include the mask in the rule data, so there is no need to store all the masks in the rule to be stored, which can reduce the occupation of RAM storage space.
[0126] Of course, it is not necessary to achieve all of the advantages described above at the same time to implement any product or method of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0127] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present invention. For ordinary technicians in this field, other embodiments can also be obtained based on these drawings.
[0128] Figure 1 A flowchart of a rule storage method provided in an embodiment of the present application;
[0129] Figure 2 A flowchart of another rule storage method provided in an embodiment of the present application;
[0130] Figure 3a A schematic diagram of a data structure of a check block provided in an embodiment of the present application;
[0131] Figure 3b A schematic diagram of the data structure of another check block provided in an embodiment of the present application;
[0132] Figure 3c A schematic diagram of a data structure of another check block provided in an embodiment of the present application;
[0133] Figure 4 An exemplary schematic diagram of a storage method of rule data provided in an embodiment of the present application;
[0134] Figure 5a A schematic diagram of the data structure of a range type check block provided in an embodiment of the present application;
[0135] Figure 5b A schematic diagram of the data structure of another range type check block provided in an embodiment of the present application;
[0136] Figure 5cA schematic diagram of the data structure of another range type check block provided in an embodiment of the present application;
[0137] Figure 6 An exemplary schematic diagram of a method for encoding a rule to be stored into a check block provided in an embodiment of the present application;
[0138] Figure 7 A flowchart of a message processing method provided in an embodiment of the present application;
[0139] Figure 8 A schematic diagram of the correspondence between the common rule table, the main rule table and the block rule table provided in the embodiment of the present application;
[0140] Fig. 9 A flowchart of another message processing method provided in an embodiment of the present application;
[0141] Fig.10 A flowchart of a method for generating a common rule table, a main rule table, and a block rule table provided in an embodiment of the present application;
[0142] Fig.11 A schematic diagram of a rule set provided in an embodiment of the present application;
[0143] Fig.12 A schematic diagram of a rule decision tree provided in an embodiment of the present application;
[0144] Fig.13 A flowchart of a method for constructing a rule decision tree is provided for an embodiment of the present application;
[0145] Fig.14 A schematic diagram of the structure of a rule storage device provided in an embodiment of the present application;
[0146] Fig.15 A schematic diagram of the structure of a message processing device provided in an embodiment of the present application;
[0147] Fig.16 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0148] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field based on this application belong to the scope of protection of the present invention.
[0149] ACL, flow table, routing table and other tables used to classify and forward messages include a series of rules for identifying messages. They are widely used in electronic devices such as routers and switches. Taking ACL as an example, electronic devices can match received messages according to the rules in ACL and process the matched messages according to pre-set policies. Electronic devices can use ACL to implement services such as message filtering, routing, network security and Quality of Service (QoS).
[0150] The ACL includes a match domain and an action domain. The match domain includes multiple match fields. The type of the match field can be a prefix type, a range type, an exact type, or a wildcard type. For example, as shown in Table 1, Table 1 exemplarily shows N rules. The match fields of each rule include source IP address, destination IP address, source port, destination port, and protocol, that is, a five-tuple of messages. The "*" in Table 1 is a wildcard.
[0151] Table 1
[0152]
[0153] When using RAM to store the above-mentioned various types of matching fields in the matching domain, the electronic device converts the various matching fields included in each rule into the form of key and mask, and the key value is used to indicate the specific value of the key.
[0154] The prefix class field includes the prefix value and the prefix length. The key is the prefix value. The first L bits of the mask are 1, and the remaining bits are 0, where L is the prefix length. For example, 10.0.8.3 / 24, 10.1.6.20 / 16, 10.0.3.8 / 24, 10.1.0.0 / 16, and 10.1.8.15 / 24 in Table 1 are all prefix class fields. Taking 10.1.0.0 / 16 as an example, the converted key is 000010100000000100000000000000000 in binary, and the mask is 111111111111111100000000000000000.
[0155] A range class field refers to a field that includes a range. If the information to be matched belongs to the range, the match is successful. For example, 0-1024 and 0-2048 in Table 1 are both range class fields. For range class fields, both key and mask are 0. Since the specific range value in the range class field can be directly stored when the range class field in the rule is stored in the embodiment of the present application, there is no need to represent the range class field equivalently as a prefix value.
[0156] Exact fields are fields that require exact matching. For example, 22 and 80 in Table 1 are both exact fields. For exact fields, the key is an exact value, and the mask is all 1. For example, the key for 80 is 1010000, and the mask is 1111111.
[0157] A wildcard field is a field in which all bits of the field are set to wildcards. For example, the "*" in Table 1 are all wildcard fields. For a wildcard field, key and mask are all 0.
[0158] When RAM is used to store rules in the related art, since RAM only supports two storage states of "0" and "1" and cannot store wildcards, it is necessary to store the key and mask of each rule. The bit width of the key and mask in the same rule is the same, resulting in higher RAM storage space, higher access bandwidth, and low search efficiency.
[0159] In order to reduce the occupation of RAM storage space, an embodiment of the present application provides a rule storage method, which is applied to an electronic device, wherein the electronic device is provided with RAM, such as Figure 1 As shown, the method includes:
[0160] S101. Obtain rules to be stored, where the rules to be stored include a matching domain, and the matching domain includes target rule data, where the target rule data is a key value and a mask in the matching domain.
[0161] Combined with the description above, a match domain of a rule may include multiple match fields. These match fields are converted into the form of key and mask, and the key of each match field included in the match domain is concatenated in sequence, and the mask of each match field is concatenated in sequence accordingly, so as to obtain the rule data included in a match domain.
[0162] For example, if the matching domain of a rule includes the source IP address field, the destination IP address field, the source port field, the destination port field and the protocol field, these fields can be converted into the form of key and mask respectively, the keys of the source IP address field, the destination IP address field, the source port field, the destination port field and the protocol field are concatenated in sequence, and the masks of the source IP address field, the destination IP address field, the source port field, the destination port field and the protocol field are concatenated in sequence, and the rule data in the matching domain of the rule includes the concatenated key and the concatenated mask.
[0163] S102: Convert the target rule data into at least one check block, and store the target rule data in the form of the check block in a RAM.
[0164] The types of check blocks include mask type and non-mask type. The rule data in the check block of mask type includes key value and mask. The rule data in the check block of non-mask type includes key value with continuous effective bits, and the effective bit is the bit with mask value 1. For example, if the key of the rule data is 00010 and the mask is 11100, the effective bits are the first 3 bits.
[0165] As an example, if the target rule data in the matching domain of the rule to be stored is converted into 5 compressed blocks, then when storing the matching domain of the rule in RAM, these 5 check blocks can be stored without storing all the keys and masks in the target rule data to achieve the purpose of compressed storage.
[0166] By adopting the above method, the electronic device can convert the target data in the matching domain of the rule to be stored into at least one check block, and store the target rule data in the form of a check block in the RAM. The types of check blocks include mask type and non-mask type. The rule data in the check block of the mask type includes a key value and a mask, and the rule data in the check block of the non-mask type includes a key value with a continuous valid bit position. It can be seen that the check block of the non-mask type does not include the mask in the rule data, so there is no need to store all the masks in the rule to be stored, which can reduce the occupation of RAM storage space.
[0167] In some embodiments of the present application, the step of obtaining the rules to be stored specifically includes:
[0168] Get the global rule set and the common rules of multiple rules included in the global rule set. Generate pending rules corresponding to each rule included in the global rule set based on the common rules, and use each pending rule as a rule to be stored; for each bit, if a rule has the same value in the bit as the common rule corresponding to the rule, the value of the pending rule corresponding to the rule in the bit is a wildcard, otherwise the value of the pending rule corresponding to the rule in the bit is the same as the value of the rule in the bit.
[0169] Optionally, the rules in the global rule set may be divided into multiple block rule tables, each block rule table includes multiple rules with the same common rules, and the electronic device may generate pending rules corresponding to each rule included in each block rule table respectively.
[0170] Among them, each rule in the global rule set is expressed in the form of key and mask.
[0171] In the embodiment of the present application, the electronic device may convert each rule in the global rule set into a binary form, and then generate pending rules based on the rules represented in the binary form.
[0172] The electronic device may convert each rule in the global rule set into binary form by:
[0173] If I is represented as any bit in the rule, the relationship between the value of bit I of each rule and the key and mask is as follows:
[0174] R[I]=0, means mask[I]=1, key[I]=0;
[0175] R[I]=1, means mask[I]=1, key[I]=1;
[0176] R[I]=* means mask[I]=0, key[I]=0 or key[I]=1.
[0177] As shown in Table 2, Table 2 shows four rules R0-R3 represented in binary form, and the common rule of R0-R3 is 10*0****.
[0178] Table 2
[0179] R0 10*0*010 R1 10*0*111 R2 10*01*01 R3 10*00*01 Public Rules Head 10*0****
[0180] For each bit I in the global rule set, a resolved rule (resolved) can be generated in the following way.
[0181] If the correctness of R[I] has been determined, then resolved[I] = R[I]; if the correctness of R[I] has not been determined, then resolved = *.
[0182] The bits whose correctness has been determined are the effective bits in the public rule, so the solved rule is the same as the public rule.
[0183] Furthermore, we can traverse the value of each bit of each rule in the global rule set to generate unresolved rules:
[0184] If R[I] is equal to resolved[I], unresolved[I] = *;
[0185] If R[I] is not equal to resolved[I], unresolved[I]=R[I].
[0186] As shown in Table 3, R is a rule in the global rule set, and resolved is the public rule corresponding to R.
[0187] At the P0 bit, R and resolved have the same value, and R[1] has been confirmed to be correct, so R[1] = resolved = 1, unresolved = *;
[0188] In the P1 bit, R and resolved have the same value, and R[2] has been confirmed to be correct, then R[1] = resolved = 0, unresolved = *;
[0189] In the P2 bit, R and resolved have the same value, and R[3] has been confirmed to be correct, so R[1] = resolved = *, unresolved = *;
[0190] In the P3 bit, R and resolved have the same value, and R[4] has been confirmed to be correct, so R[1] = resolved = 0, unresolved = *;
[0191] In the P4 bit, the values of R and resolved are different, and the correctness of R[5] is not determined, then R[1] = unresolved = 0, resolved = *;
[0192] At the P5 bit, R and resolved have the same value, and R[6] has been confirmed to be correct, so R[1] = resolved = *, unresolved = *;
[0193] In the P6 bit, the values of R and resolved are different, and the correctness of R[7] is not determined, then R[1] = unresolved = 0, resolved = *;
[0194] At the P7 bit, the values of R and resolved are different, and the correctness of R[8] is not determined, so R[1]=unresolved=1, resolved=*.
[0195] Table 3
[0196]
[0197]
[0198] According to Table 3, an unresolved rule unresolved=****0*01 can be obtained, and the unresolved rule can be used as a rule to be stored.
[0199] It should be noted that the rules to be stored can also be expressed in the form of key and mask.
[0200] After obtaining the rules to be stored, such as Figure 2 As shown, in the above S102, the target data is converted into at least one check block, which can be specifically implemented as S201-S202.
[0201] S201. Taking the first valid bit of the target rule data as the current starting bit, based on the encoding conditions satisfied by the rule data of a first number of consecutive bits starting from the current starting bit, convert the rule data of a first number of consecutive bits into a check block.
[0202] The first number is less than or equal to the CPU bit width of the electronic device and greater than a preset number. The preset number is the number of bits used to store the key value in the mask type check block.
[0203] In the embodiment of the present application, the sizes of the various check blocks are the same, which are all the CPU bit width. For example, if the CPU bit width is 64 bits, the check block size is 64 bits, and the first number is less than or equal to 64. If the CPU bit width is 32 bits, the check block size is 32 bits, and the first number is less than or equal to 32.
[0204] The preset number is the number of bits used to store the key value in the check block of the mask type, which can be set based on the check block size. A fixed portion of the bits in the check block of the mask type is used to store the offset of the start bit within the byte, and another fixed portion of the bits is used to store the type code. The remaining bits except the above two fixed bits are used to store the key value and the mask, and the number of bits used to store the key value is the same as the number of bits used to store the mask. Therefore, the preset number is the value obtained by subtracting the number of the above two fixed bits from the bit width of the check block and then dividing it by 2.
[0205] For example, if the CPU bit width is 32, the length of the check block is 32, of which 3 bits are used to store the offset, 2 bits are used to store the type code, 13 bits of the remaining 26 bits are used to store the key value, and the other 13 bits are used to store the mask, so the preset number can be 13.
[0206] It can be understood that if the rule data is represented in binary form, the first effective bit is the first bit whose value is not a wildcard, for example, the starting bit of the rule to be stored in Table 3 is P4.
[0207] Among them, S201 can be specifically implemented as follows:
[0208] Determine whether the first number of consecutive digits of rule data meets the encoding condition of non-mask type. If so, convert the key value in the first number of consecutive digits of rule data into a non-mask type check block; if not, determine that the first number is a preset number, and convert the key value and mask in the first number of consecutive digits of rule data into a mask type check block.
[0209] Among them, in the mask type check block, it is necessary to store the key value and mask of the rule data at the same time, wherein the stored key value and mask are corresponding. For example, if the rule data to be stored includes a key of 0001011111011111 and a mask of 0001010101010101, it can be seen that the 0th to 3rd bits in the mask are all 0, then it can be determined that the current starting bit is the 4th bit, and the values of the 4th to 16th bits in the key value 1011111011111 and the values of the 4th to 16th bits in the mask can be stored in a mask type check block.
[0210] Since the mask type check block stores the key and mask, the mask type encoding condition has no restrictions on the key and mask. The highest three bits in the mask type check block are used to store the offset of the current start bit within the byte, the lowest three bits are used to store the type code of the mask type, and the remaining bits are used to store: the key value and mask in the first number of consecutive bits of rule data.
[0211] Taking the CPU bit width as 32 as an example, the structure of the mask type check block is as follows Figure 3a As shown, bits 2-0 are the check block type field, which stores the type code.
[0212] 28-16 bits are used to store the key, and 15-3 bits are used to store the mask.
[0213] Bits 31-29 are the start bit field, which stores the offset of the start bit within the byte.
[0214] S202, search for the first valid bit from the bits in the target rule data that have not been converted into the check block, use the found valid bit as the current starting bit, and return to S201 until all the rule data of the valid bits included in the target rule data are converted into the check block.
[0215] By adopting the above method, the first effective bit of the target rule can be used as the current starting bit, and based on the encoding conditions satisfied by the rule data of the continuous first number of bits, the rule data of the continuous first number of bits can be converted into a check block, and after generating a check block, the starting bit of the next check block is still the effective bit, that is, the ineffective bit in the middle does not need to be stored, which can save memory space. In addition, the electronic device preferentially determines whether the rule data of the continuous first number of bits meets the encoding conditions of the non-mask type, that is, preferentially generates the check block of the non-mask type, and only generates the check block of the mask type when the encoding conditions of the non-mask type are not met. In this way, the target rule data can be converted into the check block of the non-mask type as much as possible, because the mask does not need to be stored in the check block of the non-mask type, so the storage space can be saved. In the case of not meeting the encoding conditions of the non-mask type, it can be converted into a check block of the mask type, which can ensure the accuracy of the generated check block, so the non-mask type and the mask type are used in combination, and the storage space of the RAM is saved on the basis of ensuring the accuracy of the storage of the stored rules.
[0216] The following introduces the encoding conditions of the non-mask type, where the non-mask type includes the full data type, the first length type, and the second length type.
[0217] The encoding conditions for all data types include: the current starting bit is the first bit of a byte included in the target rule data; and in the mask of the target rule data, the values of consecutive CPU bit width bits starting from the current starting bit are all 1, and the first number is the CPU bit width.
[0218] The full data type means that all bits in a check block are used to store rule data. Since all bits are used to store rule data, the check block of the full data type does not include the check block type field and the start bit field. In this way, the utilization rate of the check block of the full data type can be improved and memory space can be saved.
[0219] Among them, since the check block of the full data type does not include the check block type field, an additional storage structure is required to store the type code of the full data type. As an example, one designated bit in the byte offset field before the full data type check block can be used to store the type code of the full data type. For example, if the value of the designated bit is 1, it means that the next check block is a full data type check block. If the value of the designated bit is 0, it means that the next check block is not a full data type check block. Alternatively, the electronic device can also store the location information of each full data type check block corresponding to each rule in a separate storage area in the RAM.
[0220] Since there is no start bit field in the check block, it is impossible to identify which bit in a byte of the target rule data is the first bit stored in the check block. Therefore, an encoding condition for the full data type is that the current start bit is the first bit of a byte included in the target rule data, that is, the default full data type stores a complete byte. For example, if the CPU bit width is 32 bits, a check block can store a 4-byte key value.
[0221] One method to determine whether the start bit meets the encoding condition is to subtract 1 from the sequence number of the start bit in the rule to be stored, and then divide it by 8. If it is divisible, it means that the start bit is the first bit of a byte. For example, the key of the rule to be stored is 000000001111..., the mask is 000000001111..., and the rule to be stored can also be expressed as *******1111..., then it can be determined that the start bit is the 9th bit, 9-1 is divisible by 8, and since 1 byte includes 8 bits, it can be seen that the start bit is the first bit of the second byte in the rule to be stored.
[0222] In addition, another encoding condition for all data types is that in the mask of the rule to be stored, the values of consecutive CPU bit width bits starting from the current starting bit are all 1. In other words, consecutive CPU bit width bits are all bits that need to be matched, which can avoid storing the values of bits that do not need to be matched in the check block of the full data type, further achieving the effect of compressing the rules to be stored and saving memory space.
[0223] The encoding conditions of the first length type include: the current starting bit is the first bit position of a byte included in the target rule data; and in the mask of the target rule data, the values of a first number of consecutive bits starting from the current starting bit are all 1, the first number is smaller than the difference between the CPU bit width and the number of bits occupied by the check block type field, and the first number is greater than a preset number.
[0224] Among them, the check block type field is used to indicate the type of the check block, and the check block type field can occupy 3 bits. It can be understood that the number of bits used to store rule data in the check block of the first length type is the difference between the CPU bit width and the number of bits occupied by the check block type field. If the CPU bit width is 32, the number of bits used to store rule data in the check block is 28. The first length type can also be called a 28-bit longest match type.
[0225] As an example, the structure of the check block of the first length type is as follows: Figure 3b As shown, bits 2-0 are type codes, bits 31-3 are used for some key values in the target rule data, and these key values can be called the longest matching data.
[0226] Since there is no start bit field in the check block of the first length type, the condition that the current start bit is the first bit of a byte must also be met.
[0227] In the mask of the target rule data, the values of the first number of consecutive bit positions starting from the current starting bit are all 1, that is, in the target rule data, there are the first number of consecutive valid bits starting from the current starting bit. For example, the key value of the target storage rule data is 111110000011111, and the corresponding mask is 11111111100000000, then it can be determined that the first 8 consecutive bits in the rule data are all valid bits.
[0228] In one implementation, the electronic device may start from the current starting bit A and search the mask for the first subsequent bit B whose value is 0. It can then be determined that in the mask, the values of the bit from the starting bit A to the previous bit of the found bit position B (which may be referred to as bit C) are all 1, and the number of bits included from the starting bit A to bit C is the first number.
[0229] Taking a 32-bit CPU as an example, if the number of bits included from the start bit A to the bit C is less than 28, the number of bits included from the start bit A to the bit C is taken as the first number.
[0230] If the number of bits included from the start bit A to the bit position C is greater than 28, 28 is used as the first number.
[0231] The encoding conditions of the second length type include: the current starting bit is not the first bit of a byte included in the target rule data; and in the mask of the target rule data, the values of the first number of consecutive bits starting from the current starting bit are all 1, and the first number is smaller than the difference between the CPU bit width and the number of bits occupied by the check block type field and the number of bits occupied by the start bit field.
[0232] Among them, the start bit field is used to store the offset of the current start bit within a byte, the start bit can occupy 3 bits, the check block type can also occupy 3 bits, and the number of bits used to store rule data in the check block of the second length type is the difference between the CPU bit width and the number of bits occupied by the check block type field and the number of bits occupied by the start bit field. For example, if the CPU bit width is 32, then 32-3-3=26. Since there is another bit value in these 26 bits used to separate the effective data and the ineffective data in the rule data, the bits used to store rule data in these 26 bits are at most 25 bits, so the second length type can also be called the 25-bit longest match type.
[0233] As an example, the structure of the check block of the second length type is as follows: Figure 3cAs shown, bits 2-0 are the check block type field, which stores the type code, bits 28-3 are used to store some key values in the target rule data, which can be called the longest matching data, and bits 31-29 are the start bit field, which stores the offset of the start bit within the byte.
[0234] Since the check block of the second length type includes a start bit field, the first bit of the rule data stored in the check block may not be the first bit in a byte.
[0235] The difference between the encoding condition of the second length type and the encoding condition of the first length type is only whether the current starting bit is the first bit of a byte included in the target rule data. The other conditions are the same and will not be repeated here.
[0236] Based on the above non-mask type encoding condition, judging whether the first number of consecutive regular data bits meet the non-mask type encoding condition can be specifically implemented as follows:
[0237] If the first number of consecutive regular data bits meet any one of the encoding conditions of the full data type, the first length type and the second length type, it is determined that the encoding condition of the non-mask type is met; otherwise, it is determined that the encoding condition of the non-mask type is not met.
[0238] By adopting the above method, it is possible to determine whether the first number of consecutive regular data bits starting from the current starting bit meet the encoding conditions of the full data type, the first length type, and the second length type, so that the most appropriate encoding method can be selected to compress and store the target regular data. When any of the encoding conditions of the full data type, the first length type, and the second length type are met, there is no need to store the mask of the corresponding bit, which can save storage space.
[0239] Based on the encoding conditions of the full data type, the first length type, and the second length type introduced in the above embodiment, the implementation method of converting the key value in the regular data of the continuous first number of bits into a non-mask type check block may specifically include the following three cases:
[0240] Case 1: If the first number of consecutive regular data bits meet the encoding condition of the full data type, the key value in the first number of consecutive regular data bits is converted into a check block of the full data type. Accordingly, the check block of the full data type is used to store the key value in the first number of consecutive regular data bits.
[0241] Case 2: if the first number of consecutive regular data bits meet the encoding condition of the first length type and do not meet the encoding condition of the full data type, the key value in the first number of consecutive regular data bits is converted into a check block of the first length type.
[0242] Correspondingly, the lowest three bits of the check block of the first length type are used to store the type code of the first length type, and from the highest bit to the fourth to last bit are used to store: the key value in the rule data of the first number of bits and a 0, and the value of the unoccupied bit is set to 1 by default.
[0243] It can be understood that since all bits in the check block of all data types can be used to store rule data, the check block of all data types has the highest utilization rate. Therefore, when the encoding conditions of all data types and the first length type are met at the same time, the check block of all data types is used for storage first, which can make more effective use of RAM storage resources.
[0244] The structure of the check block of the first length type can be referred to Figure 3b , Figure 3b The longest matching data part in is stored as follows Figure 4 shown.
[0245] The first number is the number of bits in the rule data to be encoded this time whose mask value is 1. The first number is represented as P. Figure 4 The first P bit positions in the target rule data are filled with the key values of the P bit positions starting from the current start bit in order. Then the P+1th bit position is filled with 0, and all subsequent bits are filled with 1.
[0246] Figure 4 In the example, the first P bits are valid data, and the data after 0 are all don't care data.
[0247] Case 3: if the first number of consecutive regular data bits meet the encoding condition of the second length type, the first number of consecutive regular data bits are converted into a check block of the second length type.
[0248] Correspondingly, the highest three bits in the check block of the second length type are used to store the offset of the current starting bit within the byte, the lowest three bits are used to store the type code of the second length type, and the remaining bits are used to store in sequence: the key value in the rule data of the first number of consecutive bits and a 0, and the unoccupied bits are set to 1 by default.
[0249] The structure of the second length type check block can be referred to Figure 3c , Figure 3c The longest matching data part in can also be stored as follows Figure 4 Storage method shown.
[0250] The first number is the number of bits whose mask value is 1 in the rule data to be encoded this time. The first number is represented as P. Figure 4The first P bits are filled with the key values of the P bits starting from the current start bit in the target rule data in order. Then the P+1th bit is filled with 0, and all subsequent bits are filled with 1.
[0251] The offset of the current start bit within the byte is the remainder of the position S of the value of the current start bit in the target rule data divided by 8. For example, if S is 12, the offset within the byte is 4.
[0252] It can be seen that in the above three cases, there is no need to store the mask of the target rule data, which can save memory space and improve RAM resource utilization.
[0253] In the embodiment of the present application, the target rule data also includes range-type rule data, which is not converted into the form of key and mask. The range-type rule data specifically includes the value of the range class field in the matching domain. In the embodiment of the present application, the encoding method for the range-type rule data is as follows:
[0254] If the target rule data includes range type rule data, and the maximum and minimum values of the range type rule data are both less than 2 13 , then convert the range type rule data into the first range type check block; if the minimum value of the range type rule data is greater than or equal to 2 13 , or the maximum value is greater than or equal to 2 13 , the range type rule data is converted into a second range type check block and a third range type check block.
[0255] Among them, the highest 13 bits of the check block of the first range type are used to store the maximum value, the subsequent 13 bits are used to store the minimum value, the subsequent 2 bits are used to store the first subtype code, and the lowest 3 bits are used to store the range type code, and the first subtype code is used to indicate that the check block includes a matching range.
[0256] If the maximum and minimum values of the range type rule data are both less than 2 13 , then the storage space of a check block is sufficient to store both the maximum and minimum values, such as Figure 5a As shown, 31-19 bits are used to store the maximum value (Max), 28-16 bits are used to store the minimum value (Min), 5-4 bits are sub, and the value of sub is the first subtype code. 3 bits are not occupied yet, and 2-0 bits are the check block type, which is specifically used to store the type code of the range type.
[0257] The highest 26 bits of the second range type check block are used to store the minimum value, the following two bits are used to store the second subtype code, and the lowest 3 bits are used to store the range type code, and the second subtype code is used to indicate that the check block includes a minimum value of a matching range.
[0258] The highest 26 bits of the third range type check block are used to store the maximum value, the following two bits are used to store the third subtype code, and the lowest 3 bits are used to store the range type code. The third subtype code is used to indicate that the check block includes a maximum value of a matching range.
[0259] If the minimum value (Min) of the range type rule data is greater than or equal to 2 13 , or the maximum value (Max) is greater than or equal to 2 13 , two check blocks can be used to store the maximum value (Max) and minimum value (Min) of the range type rule data respectively.
[0260] like Figure 5b As shown, 31-6 bits are used to store the minimum value (Min), 5-4 bits are sub, the value of sub is the second subtype code, 3 bits are not occupied yet, and 2-0 bits are the check block type, which is specifically used to store the type code of the range type.
[0261] like Figure 5c As shown, 31-6 bits are used to store the maximum value (Max), 5-4 bits are sub, the value of sub is the third subtype code, 3 bits are not occupied yet, and 2-0 bits are the check block type, which is specifically used to store the range type code.
[0262] It can be seen that in the embodiment of the present application, there is no need to divide the range-type rule data into multiple sub-ranges and convert each sub-range into a prefix-type field respectively, which can avoid the expansion caused by the range-type rule data and further save storage space.
[0263] In order to determine the position of the rule data stored in the check block in the target storage rule, a byte offset field is included before each check block stored in the RAM. The byte offset field before each check block is used to indicate the byte offset of the starting bit of the rule data in the check block in the target rule data.
[0264] For example, if the byte offset is 3, it means that the starting bit in the check block is at the third bit in a byte in the target rule data of the rule to be stored.
[0265] like Figure 6 As shown, Figure 6An exemplary schematic diagram of a rule storage method provided in an embodiment of the present application, starting from the lowest bit, searches for the first bit S0 that is not *.
[0266] The 13 bits starting from S0 meet the encoding condition of the mask type, and the 17 bits starting from S0 meet the encoding condition of the 25-bit longest match type, and the 25-bit longest match type is the second length type in the above embodiment.
[0267] At this time, according to the longest match principle, the 25-bit longest match type with a longer encoding length can be selected, and the 17-bit key value after S0 is stored in the check block of the second length type. S0 / 8 before the check block is the byte offset of the check block in the rule to be stored. Among them, S0 / 8 refers to the quotient obtained by dividing S0 by 8.
[0268] Continue to search for the second bit S1 that is not *. The 13 bits starting from S1 meet the encoding conditions of the mask type, and the 4 bits after S1 meet some conditions in the 25-bit longest match type. However, because the first number N=4 is less than the preset number 13, the condition that the first number is greater than the preset number is not met, and the value of the 13 bits after S1 can be stored in the check block of the mask type. S1 / 8 before the check block is the byte offset of the check block in the rule to be stored. Among them, S1 / 8 refers to the quotient obtained by dividing S1 by 8.
[0269] Based on the same inventive concept, the embodiment of the present application also provides a message processing method, which is applied to an electronic device, in which a RAM is provided, and the target rule data in the matching domain of each rule in the designated rule table of the RAM is stored in the form of a check block, and the types of the check blocks include mask type and non-mask type, and the rule data in the check block of the mask type includes a key value and a mask, and the rule data in the check block of the non-mask type includes a key value with continuous effective bits, and the effective bit is a bit with a mask value of 1. Among them, each rule in the designated rule table of the RAM can be specifically stored according to the rule storage method introduced in the above embodiment.
[0270] like Figure 7 As shown, the message processing method includes:
[0271] S701. Extract and search key values from the message to be processed.
[0272] In an embodiment of the present application, after obtaining the message to be processed, the electronic device can extract key information constituting the key from the header of the message to be processed, and use the extracted key information to form a search key, that is, a search key value.
[0273] For example, in Table 1, the matching domain of each rule includes a message quintuple. The electronic device can extract the quintuple from the message to be processed, and arrange the extracted quintuple in the order of source IP address, destination IP address, source port, destination port and protocol to obtain the search key value.
[0274] In an embodiment of the present application, the order of the fields in the search key is not limited, as long as the order of the fields in the search key is consistent with the order of the fields in the rule. For example, the rule includes four fields: source IP, destination IP, source port, and destination port. The order of these four fields is: source IP, destination IP, source port, and destination port; then the order of the fields in the search key is also: source IP, destination IP, source port, and destination port.
[0275] S702: Determine a rule to be matched in a specified rule table based on the search key value.
[0276] S703: For each check block in the to-be-matched rule, determine the length of the to-be-matched data according to the type of the check block.
[0277] In an embodiment of the present application, the rules to be matched stored in the RAM may include multiple check blocks, and the electronic device may determine the length of the data to be matched based on the type of each check block, and the length of the data to be matched is the number of bits to be matched in the check block. For example, if there are 8 bits of rule data in the check block that need to be matched, the length of the data to be matched is 8 bits.
[0278] S704, according to the starting position of the rule data included in the check block, starting from the same starting position in the search key value, extracting the data to be matched according to the length of the data to be matched, and matching the data to be matched with the rule data included in the check block.
[0279] It is understandable that a rule can be stored as multiple check blocks, each check block includes part of the data in the rule, and the starting bit of the rule included in a check block refers to: the bit position occupied by the first bit included in the check block in the target rule data of the entire rule.
[0280] For example, if the target rule data of a rule includes 30 bits, and a check block stores data of bits 8 to 16 in the target rule data of the rule, the starting bit of the check block is the 8th bit.
[0281] Accordingly, when matching the search key value with the rule, data may be extracted starting from the 8th bit position of the search key value, that is, the data to be matched is the data from the 8th to the 16th bits in the search key value.
[0282] S705: If the search key value successfully matches all the check blocks in the rule to be matched, it is determined that the search key value successfully matches the rule to be matched.
[0283] Each rule to be matched includes at least one check block. Only when the search key value and all the check blocks are successfully matched, can it be determined that the message to be processed matches the rule to be matched.
[0284] By adopting the above method, in the embodiment of the present application, the rules are stored in the form of check blocks, and the types of check blocks include mask type and non-mask type. The rule data in the check block of the mask type includes a key value and a mask, and the rule data in the check block of the non-mask type includes a key value with continuous effective bits. It can be seen that the check block of the non-mask type does not include the mask of the rule, so there is no need to store all the masks of a rule. When matching the rules in the RAM later, the data to be matched in the key value to be found can be matched with the check block in the rule to be matched. It can be seen that storing the rule data in the rule to be matched in the form of a check block can also achieve matching of the key value to be found, so that the occupation of RAM storage space can be saved.
[0285] In addition, when matching the rules in the RAM, the data to be matched in the search key value can be matched with the check block in the rule to be matched. Since the non-mask type check block does not include a mask and the data volume is small, the bandwidth of the electronic device when accessing the RAM can be reduced and the search efficiency can be improved.
[0286] The embodiments of the present application can be applied to the following two scenarios.
[0287] Scenario 1: Use RAM to store all rules in the global rule set.
[0288] In this scenario, each rule in the global rule set can be converted into the form of a key and a mask, and then each rule can be used as a rule to be stored. The target rule data in each rule to be stored can be converted into a check block and stored in RAM in the form of a check block. Compared with storing the key and mask of each rule, memory space can be saved.
[0289] Optionally, multiple block rule tables may be stored in the RAM, and multiple rules in each block rule table have the same common rule. A linear table may also be stored in the RAM, and the linear table includes multiple base addresses, and the base addresses are used to locate the block rule table.
[0290] The multiple rules included in each block rule table have the same bit value within a preset range. For example, the first 8 bits of the mask of each rule in a block rule table are all 1, and the keys are all the same.
[0291] The bits in the preset range may be the first 8 bits of the source IP address of each rule, or the first 8 bits of the destination IP address, or a range of bits corresponding to other fields, and there is no limitation on this.
[0292] The electronic device can use the key of the bits in the preset range in each block rule table as a base address for locating the block rule table. The base addresses in the linear table can be arranged in a certain order, such as from small to large.
[0293] In scenario one, the above S702 can be specifically implemented as follows: using the bits within a preset range in the search key value as the search index, determining the target base address pointed to by the search index in the linear table; locating the block rule table based on the target base address, and using the located block rule table as the designated rule table; and using the rules included in the designated rule table as the rules to be matched.
[0294] For example, if the preset range is the first 8 bits of the entire rule, the first 8 bits of the search key value may be extracted as the search index.
[0295] Since the base addresses in the linear table are arranged in order, the target base address pointed to by the search index can be quickly located based on the search index, and then the block rule table can be located using the target base address, and the located block rule table can be used as the index to be matched. This is equivalent to a coarse-grained screening of the rules in the RAM first, so that there is no need to match the search key value with all the rules in the RAM one by one, but only to match the rules in the block rule table corresponding to the target base address, which can save matching time and improve processing efficiency.
[0296] Optionally, there may be multiple preset ranges, for example, multiple rules in a block rule table have the same bit values within the first preset range, and the same bit values within the second preset range. There may also be a situation where multiple rules in two block rule tables have the same bit values within the first preset range.
[0297] For example, the first preset range may be the first 8 bits of the source IP address, and the second preset range may be the first 8 bits of the destination IP address.
[0298] In response to the above situation, multiple linear tables can be set in the RAM, each linear table corresponds to a preset range, and then when performing coarse-grained screening on the search key value, multi-level screening can be performed. For example, the value of the bit within the first preset range is first extracted from the search key value as the first index, and the block rule table is located based on the first target base address pointed to by the first index in the first linear table. At this time, multiple block rule tables may be located. For the convenience of description, the multiple block rule tables are called set A.
[0299] Furthermore, the value of the bit in the second preset range is extracted from the search key value as the second index, and the block rule table is located in the above set A based on the second target base address pointed to by the second index in the second linear table, which is equivalent to completing the second level of screening, and then the block rule table located this time is used as the designated rule table. In this way, the number of designated rule tables located can be reduced, and the number of rules to be matched that need to be matched later can be reduced, which can further improve the processing efficiency.
[0300] It should be noted that the embodiment of the present application does not limit the number of preset ranges and the number of linear tables, and can be flexibly set based on the number of rules in the global rule set.
[0301] As an optional implementation, after the rules in the specified rule table are used as the rules to be matched, the subsequent Figure 7 When the process shown matches the to-be-matched rules one by one, the bits within the preset range in the to-be-matched rules may no longer be matched, thereby avoiding repeated matching of the bits within the preset range and improving processing efficiency.
[0302] Scenario 2: Use TCAM and RAM to store ACL.
[0303] In scenario 2, the electronic device is also provided with a TCAM, in which a public rule table is stored, and a main rule table and a block rule table are stored in the RAM, and a plurality of rules having the same public rule are stored in the block rule table. The main rule table includes at least two fields, a split position set and a base address. The rules in the block rule table belong to a subtree determined by the split position set. The split position set is used to locate the position of the rule corresponding to the key value in the block rule table. The base address is used to locate the block rule table. The value of the non-wildcard bit in the public rule is the same as the value of the same bit of all rules in the block rule table pointed to by the base address. The public rule table stores public rules, and the index of the public rule table and the main rule table corresponding to the same block rule table is the same.
[0304] The rules in the block rule table may be stored in the form of check blocks according to the rule storage method described in the above embodiment.
[0305] In the embodiment of the present application, one or more public rule tables, one or more main rule tables, and one or more block rule tables may be stored in the electronic device. The public rule table and the main rule table correspond one to one. For the table items of the public rule table and the table items of the main rule table with the same index, both correspond to the same block rule table. One table item in the main rule table corresponds to one block rule table. When the main rule table includes multiple table items, the main rule table corresponds to multiple block rule tables. Taking a public rule table and a main rule table as an example, the correspondence between the public rule table, the main rule table, and the block rule table is as follows: Figure 8 As shown in the figure, the index is used to indicate the position of the entry in the table.
[0306] The common rule table is established in TCAM, which can also be called TCAM table. An entry in the TCAM table is a common rule of multiple rules in a block rule table, such as Figure 8 In the embodiment of the present application, multiple rules in a block rule table constitute a rule block.
[0307] A TCAM table has a corresponding master rule table stored in RAM, such as Figure 8 L0-Ln in the main rule table. The main rule table can be a linear table or other forms of tables supported by RAM. The main rule table can also be called a main table. A table entry in the main table stores the split position set (split bits) of a rule block and the base address of the subordinate table. Here, the subordinate table is the block rule table storing the rule block. The offset of each rule block corresponding table entry in the main table is the same as the offset of the rule block corresponding table entry in the TCAM table. The offset here is the index, such as Figure 8 The primary index in .
[0308] The multiple rules included in a rule block constitute a block rule table and are stored in RAM, such as Figure 8 Rules 0-3 in constitute a rule block, which is stored in block rule table 0 in RAM. The block rule table can be a linear table or other form of table supported by RAM. The block rule table can also be called a block table, such as Figure 8Block table 0 and Block table n in the main table. The base address and split position set of a block table are stored in a table entry in the main table. The split position set consists of multiple bits. Assuming that the number of bits included in the split position set is S, the size of the Block table is 2 to the power of S, that is, the Block table includes 2 to the power of S rules. For example, S=2, 2 to the power of 2=4, then the Block table includes 4 rules. When the electronic device obtains the split position set of a rule block, it extracts the value of each rule in the rule block in these S bits to form the offset of each rule. The offset is the index of the rule in the block table, such as Figure 8 The sub-index in .
[0309] It should be noted that, to facilitate TCAM storage, the electronic device can convert the rules in the global rule set into the form of key and mask, and then convert the rules represented by key and mask into binary form. The conversion method has been introduced in the above embodiment and will not be repeated here.
[0310] As well as the common rule table stored in the TCAM and the main rule table and block rule table stored in the RAM, such as Fig. 9 As shown, the above S702, determining the to-be-matched rule in the specified rule table based on the search key value, can be specifically implemented as follows:
[0311] S7021. Use the search key value to match in the public rule table to obtain the primary index of the target table item in the main rule table.
[0312] The electronic device can use the search key value to match in the public rule table to obtain the index of the table item where the matched public rule is located, and the index is the index of the target table item in the main rule table. In the embodiment of the present application, the index of the target table item in the main rule table is called the main index. In the embodiment of the present application, the search key value matches the public rule, which can be understood as: the search key value is the same as the value of the public rule in the same bit, or the value of the public rule in the same bit is *.
[0313] S7022. Locate the block rule table based on the base address included in the target table entry pointed to by the primary index, and use the located block rule table as the designated rule table.
[0314] The public rule table corresponds to the main rule table one by one, and the index of the public rule table and the main rule table corresponding to the same block rule table is the same. After obtaining the main index from the TCAM, the electronic device reads the target table entry corresponding to the main index in the main rule table, obtains the base address and split position set of the block rule table, and then locates the block rule table at the base address, and uses the block rule table as the designated rule table.
[0315] S7023. Determine the secondary index of the to-be-matched rule in the specified rule table based on the search key value and the split position set included in the target table entry pointed to by the primary index.
[0316] As described in step S7022, the electronic device reads the target table entry corresponding to the main index in the main rule table, and can obtain the base address and split position set of the block rule table. The electronic device extracts the value of each bit shown in the read target split position set from the search key value, and the extracted values of each bit constitute a secondary index, which is the offset of the matched rule in the located block rule table. The matched rule in the located block rule table is the rule to be matched.
[0317] The execution order of step S7022 and step S7023 is not limited in the embodiment of the present application.
[0318] S7024. Use the rule pointed to by the secondary index as the rule to be matched.
[0319] In an embodiment of the present application, N TCAM tables may be stored in the electronic device, where N is greater than or equal to 1. For each TCAM table, the electronic device may obtain a rule to be matched based on the TCAM table, the main table corresponding to the TCAM table, and the Block table corresponding to the TCAM table. In this way, the electronic device may obtain at most N rules to be matched from the N TCAM tables.
[0320] It is understandable that after obtaining the rules to be matched, the rules to be matched need to be verified, that is, through Figure 7 The method flow shown matches the search key value with the check block of the rule to be matched.
[0321] In some embodiments of the present application, if there is one rule to be matched and the search key value successfully matches the rule to be matched, the message to be processed is processed according to the action domain corresponding to the rule to be matched; if the search key value does not match the rule to be matched, the electronic device refuses to use the rule to be matched to process the message to be processed.
[0322] If there are multiple rules to be matched, the search key value is matched with each rule to be matched respectively, and the message to be processed is processed according to the action field corresponding to the rule to be matched that successfully matches the search key value.
[0323] If there are multiple rules to be matched that successfully match the search key value, a rule to be matched with the highest priority can be selected to process the message to be processed.
[0324] Since the rules in the specified rule table in the embodiment of the present application are stored in the form of check blocks, and not all the rule data of each rule is stored, during the verification process, it is only necessary to match the search key value with the check block corresponding to the rule to be matched, and no integrity check is required, which can reduce the access bandwidth to the RAM and improve the efficiency of message processing. In addition, a TCAM and a RAM are provided in the electronic device in the embodiment of the present application. The electronic device stores the common rules of multiple rules in the TCAM and stores the specific rules in the RAM, which can reduce the demand for the TCAM capacity. The electronic device uses the common rules stored in the TCAM with higher search performance to exclude most of the rules, and subsequently uses the check block of the rule to be matched stored in the RAM to match the search key value, which can realize the use of a smaller capacity TCAM and RAM to achieve a larger scale of rule search, saving the cost and power consumption of message processing.
[0325] In some embodiments, for each block rule table, the common rules corresponding to the block rule table may be generated in the following manner:
[0326] If the values of one bit of all the rules stored in the block rule table are 1, the value of the bit of the common rule corresponding to the block rule table is set to 1.
[0327] If the values of one bit of all the rules stored in the block rule table are all 0, the value of the bit of the public rule corresponding to the block rule table is set to 0.
[0328] If the values of one bit of all rules stored in the block rule table are not all 1, and / or the values of one bit of all rules stored in the block rule table are not all 0, the value of the bit of the public rule corresponding to the block rule table is set to a wildcard.
[0329] As shown in Table 4, Table 4 shows 4 rules included in a block rule table, namely rules 0-3. At bit position P3, the values of rules 0-3 are all 0, then the electronic device sets the value of bit position P0 of the public rule to 0; at bit position P7, the values of rules 0-3 are all 1, then the electronic device sets the value of bit position P1 of the public rule to 1; at other bit positions, if rules 0-3 do not meet the above two conditions, then the electronic device sets the values of these bits of the public rule to *.
[0330] Table 4
[0331] Public Rules * * * 0 * * * 1 Rule 0 * 1 * 0 * 0 1 1 Rule 1 * * 0 0 * 1 1 1 Rule 2 * * * 0 1 * 0 1 Rule 3 * * * 0 0 * 0 1 bit P0 P1 P2 P3 P4 P5 P6 P7
[0332] In the embodiment of the present application, the bit width of the common rule is the same as the bit width of the rule. When all the bits of the common rule are set, the common rule is generated. The generated common rule is stored in the TCAM, and a corresponding main rule table entry and a block rule table storing all rules corresponding to the common rule are established in the RAM.
[0333] In the technical solution provided in the embodiment of the present application, the electronic device extracts the common rules of all rules in each block rule table, that is, one common rule can represent multiple rules, and stores the common rules in the TCAM, thereby reducing the demand for TCAM capacity. A smaller capacity TCAM can be used to implement larger-scale rule search, saving the cost and power consumption of message processing.
[0334] In some embodiments, the present application also provides a method for generating a common rule table, a main rule table, and a block rule table, such as Fig.10 As shown, Fig.10 A first flow chart of a method for generating a common rule table, a main rule table, and a block rule table provided in an embodiment of the present application, the method may include the following steps:
[0335] S1001, constructing a rule decision tree based on the balance bit of each rule in the global rule set, where the balance bit is the bit with the smallest difference between the number of rules with a value of 0 and the number of rules with a value of 1.
[0336] In an embodiment of the present application, the electronic device takes all rules with a matching domain and an action domain as a global rule set. Considering the entire rule, the electronic device determines the balance bit of the rules in the global rule set. At the balance bit, the difference between the number of rules with a value of 0 and the number of rules with a value of 1 is the smallest.
[0337] For example, the rule set in the electronic device includes Fig.11 The five rules R1-R5 shown are from Fig.11 It can be seen that at bit position P2, the number of rules with a value of 0 is 2, the number of rules with a value of 1 is 2, and the difference between the two is 2-2=0. This is the bit position where the difference between the number of rules with a value of 0 and the number of rules with a value of 1 is the smallest. Therefore, it can be determined that bit position P2 is a balanced bit position.
[0338] The electronic device constructs a rule decision tree based on the balance position of each rule in the global rule set. Among them, the rule decision tree can be implemented by a decision tree in the form of a binary tree. The rule decision tree in the form of a binary tree is used to manage the rule set, so that the technical solution provided by the embodiment of the present application is more general and has nothing to do with specific applications. In the embodiment of the present application, the electronic device can also use other forms of rule decision trees, which are not limited to this.
[0339] S1002, starting from each leaf node of the rule decision tree, traverse upward in breadth-first order to obtain the deepest rule subtree corresponding to each leaf node, the deepest rule subtree being the deepest rule subtree among the rule subtrees whose balance bit is less than or equal to the preset splitting bit depth.
[0340] A maximum number of split bits is preset in the electronic device, that is, a preset split bit depth, which is the maximum number of bits included in the split position set. The size of the preset number can be set according to actual needs.
[0341] After a rule decision tree is constructed, for each leaf node of the rule decision tree, the electronic device traverses upward from the leaf node in a breadth-first manner. When traversing to a node of the rule decision tree, this node is used as the root node to form a subtree; if the electronic device determines that the subtree is the deepest subtree whose number of balance bits is less than or equal to the preset split bit depth, the subtree is used as the deepest rule subtree and the upward traversal is stopped; otherwise, this node is used as the starting point to continue the upward traversal.
[0342] When all leaf nodes of the rule decision tree belong to a deepest rule subtree respectively, the electronic device stops executing the above step S1002.
[0343] Step S1003: Generate a common rule table, a main rule table and a block rule table based on each deepest rule subtree.
[0344] In the embodiment of the present application, after the electronic device executes step S1002, it obtains one or more deepest rule subtrees, and generates a block rule table based on the rule set included in each deepest rule subtree; generates a split position set based on the balance bit included in each deepest rule subtree; generates a main rule table based on the split position set corresponding to each deepest rule subtree and the base address of the block rule table; generates a common rule table based on the table entry index in the main rule table corresponding to each deepest rule subtree and the common rules of the rule set included in each deepest rule subtree.
[0345] Among them, all the rules included in a deepest rule subtree constitute a block rule table, that is, a rule block. For a deepest rule subtree, the electronic device generates a block rule table from the rule set included in the deepest rule subtree; generates a split position set from the balance bits included in other nodes except the root node of the deepest rule subtree; generates an entry in the main rule table from the split position set corresponding to the deepest rule subtree and the base address of the block rule table; generates an entry in the public rule table from the common rules of the rule set included in the deepest rule subtree, and the index of the entry in the public rule table corresponding to the deepest rule subtree is the same as the index of the entry in the main rule table corresponding to the deepest rule subtree. A main rule table is generated from the entries in the main rule table corresponding to all the deepest rule subtrees included in a rule decision tree; a public rule table is generated from the entries in the public rule table corresponding to all the deepest rule subtrees included in a rule decision tree.
[0346] For example, the default split bit depth is 3, such as Fig.12 The rule decision tree shown in the figure, where R0-R3 represent the rules included in the leaf nodes, and P0-P6 represent the balance bits included in the nodes. The electronic device traverses upward from the leaf node R0 in a breadth-first manner, traverses to the node P2, and takes P2 as the root node to form a subtree. The subtree includes rules R0 and R1. The number of balance bits included in the subtree is 0, 0<3. Continuing to take node P2 as the starting point, continue to traverse upward to bit position P1, and take P1 as the root node to form a subtree. The balance bits included in the subtree are: P2 and P3, and the corresponding rules are: rules R0-R3. At this time, the number of balance bits included in the subtree is 2, 2<3. If the node P1 is taken as the starting point, the subtree is continued. Continue to traverse upward, the number of balancing bits included in the formed subtree must be greater than 3. Therefore, the subtree formed with node P1 as the root node is the deepest rule subtree with the number of balancing bits not greater than 3. The split position set of the deepest rule subtree is {P2, P3}, and the rules R0-R3 are stored in a block rule table 1. Based on the split position set {P2, P3} and the base address of the block rule table 1, an entry 1 in the main rule table is generated; based on the index of the entry 1 in the main rule table and the common rules of the rules R0-R3, an entry 2 in the common rule table is generated.
[0347] In the embodiment of the present application, the electronic device uses the above steps S1001-S1003 to generate a common rule table, a main rule table and a block rule table. In order to avoid the problem of repeated storage of rules, a balance bit is recorded only once in all split positions of a rule decision tree. In this way, the capacity of TCAM and RAM can be effectively utilized, saving costs.
[0348] In the technical solution provided by the embodiment of the present application, the electronic device can extract more common information of the rules by using the rule decision tree, thereby saving costs, reducing the difficulty of finding the rules, and improving the search efficiency.
[0349] In some embodiments, Fig.13 As shown, the above step S1001 can be refined into the following steps:
[0350] Step S1301: Use the global rule set as the rule set to be split, and the global rule set is included in the root node of the rule decision tree.
[0351] Step S1302, determining the balance bit of each rule in the rule set to be split. The method of determining the balance bit in step S1302 can refer to the relevant description of step S1001.
[0352] Step S1303: Add the rule whose balance bit value is 0 in the to-be-split rule set to the first side child node, and add the rule whose balance bit value is 1 in the to-be-split rule set to the second side child node.
[0353] Taking the rule decision tree in the form of a binary tree as an example, the first side is left and the second side is right, or the first side is right and the second side is left. Taking the first side as left and the second side as right as an example, combined with Fig.11 Take this as an example. The electronic device determines that the balance bit is P2, divides the rules R2 and R5 with the value of the bit P2 as 0 into a rule set 1, and adds the rule set 1 to the left child node of the rule decision tree; divides the rules R1 and R4 with the value of the bit P2 as 1 into a rule set 2, and adds the rule set 2 to the right child node of the rule decision tree. At this point, the splitting of one rule set into two rule sets is completed.
[0354] In the embodiment of the present application, there is a case where the value of the balance bit is "*", such as Fig.11 In the example, the value of the balance bit P2 of rule R3 is "*". In this case, the electronic device does not process such rules at all, and such rules can also be added to the discard rule set for subsequent processing. There is no limitation on this.
[0355] Step S1304: for each child node obtained by splitting, if the child node includes multiple rules, the rule set included in the child node is used as the rule set to be split, and step S1302 is executed again until each child node obtained by splitting includes one rule, thereby obtaining a rule decision tree.
[0356] In the embodiment of the present application, the electronic device executes steps S1302-S1304 in a loop, and the condition for the loop to end is: the child node obtained by splitting includes a rule. A rule decision tree in the form of a binary tree is used to manage the rule set, and a leaf node of the rule decision tree includes a rule. Fig.12 The structure of a rule decision tree is shown, and each of the four leaf nodes R0-R3 of the rule decision tree includes only one rule.
[0357] In the embodiment of the present application, the electronic device uses the above steps S1301-S1304 to construct a rule decision tree, and then the rule decision tree can generate a common rule table, a main rule table and multiple block rule tables. In the technical solution provided by the embodiment of the present application, the electronic device can extract more common information of the rules by using the rule decision tree, while saving costs, reducing the difficulty of finding rules and improving the search efficiency.
[0358] In some embodiments, the electronic device may also evenly divide the global rule set into multiple rule sets, each rule set includes multiple rules stored as a block rule table, and extract the common rules of this block rule table and the corresponding split position set to generate the corresponding common rule table and main rule table.
[0359] Based on the storage structure of each type of check block introduced in the above embodiments, Figure 7 In the corresponding embodiment, the implementation method of determining the length of the data to be matched according to the type of the check block specifically includes the following situations:
[0360] Case 1: If the type of the check block is a mask type, the length of the data to be matched is determined to be the length of a preset number of bit positions.
[0361] For example, taking the CPU bit width as 32 bits, see Figure 3a , where key and mask each occupy 13 bits, so the default number is 13.
[0362] Case 2: If the type of the check block is the full data type, the length of the data to be matched is determined to be the CPU bit width of the electronic device.
[0363] Since all bits in the check block of all data types are used to store rule data, the length of the data to be matched is the check block size, that is, the CPU bit width.
[0364] Case 3: If the type of the check block is the first length type or the second length type, search for the first bit position with a value of 0 from the lowest bit of the rule data included in the check block toward the highest bit, and determine that the length of the data to be matched is the length from the highest bit to the bit found.
[0365] Among them, since only valid data needs to be matched, see Figure 4 , Figure 4 It is a schematic diagram of the storage structure of the rule data part in the first length type and the second length type. The data before the bit with a value of 0 is valid data, so the bit length before the bit with a value of 0 is the length of the data to be matched.
[0366] Case 4: If the type of the check block is a range type, the length of the data to be matched is determined to be a fixed length of the range type field.
[0367] Among them, if the type of the check block is a range type, the value of the sub field in the check block can be further obtained. If the value is the first subtype code, the corresponding fixed length is 13 bits; if the value is not the first subtype code, the corresponding fixed length is 26 bits.
[0368] Furthermore, matching the data to be matched with the rule data included in the verification block can be specifically implemented as follows:
[0369] If the type of the check block is a mask type, the key value and the mask in the check block are ANDed, and the result obtained after the ANDed operation is compared with the data to be matched. If they are completely consistent, the match is successful.
[0370] For a mask-type check block, the electronic device can obtain the byte offset from the byte offset field in the check block, and in combination with the value of the start bit field in the check block, can determine the start bit of the rule data included in the check block. For example, if the determined start bit is the 7th bit of the 1st byte, and the length of the data to be matched is 13 bits, the 13-bit data to be matched can be extracted starting from the 7th bit of the 1st byte of the search key value, and the 13-bit key and the 13-bit mask in the check block are ANDed, and then the result of the ANDed operation is compared with the 13-bit data to be matched. If they are completely consistent, the match is successful.
[0371] If the type of the check block is the full data type, the data to be matched is compared with all the rule data in the check block. If they are completely consistent, the match is successful.
[0372] For the check block of all data types, the electronic device can obtain the byte offset from the byte offset field in the check block, and then determine the starting bit of the rule data included in the check block. For example, if the determined starting bit is the third byte, the 32-bit data to be matched is extracted from the third byte of the search key value, and compared with the 32-bit rule data in the check block. If they are completely consistent, the match is successful.
[0373] If the type of the check block is the first length type or the second length type, starting from the highest bit of the rule data included in the check block, the rule data is extracted according to the length of the data to be matched, and the extracted rule data is compared with the data to be matched. If they are completely consistent, the match is successful.
[0374] If the type of the check block is a range type, the matching range is obtained from the check block. If the data to be matched belongs to the matching range, the match is successful.
[0375] By adopting the above method, the rule data in the check block can be matched with the rule data to be matched according to the type of each check block. Since the check block is encoded in a compressed manner, the access bandwidth can be reduced and the matching speed can be improved. According to experiments, by adopting the method provided in the embodiment of the present application, a 40-byte ACL can be compressed to one-half of the original, and an 80-byte ACL can be compressed to one-quarter of the original, which greatly saves storage space.
[0376] Based on the same inventive concept, an embodiment of the present application provides a rule storage device, which is applied to an electronic device, wherein a RAM is provided in the electronic device, such as Fig.14 As shown, the device comprises:
[0377] The acquisition module 1401 is used to acquire the rules to be stored, wherein the rules to be stored include a matching domain, wherein the matching domain includes target rule data, and the target rule data is a key value and a mask in the matching domain;
[0378] A conversion module 1402, configured to convert the target rule data into at least one check block;
[0379] The storage module 1403 is used to store the target rule data in the RAM in the form of a check block;
[0380] Among them, the types of the check blocks include mask type and non-mask type, the rule data in the check blocks of the mask type include key values and masks, and the rule data in the check blocks of the non-mask type include key values with continuous valid bits, and the valid bits are bits with mask values of 1.
[0381] In one implementation, the conversion module 1402 is specifically configured to:
[0382] Taking the first valid bit of the target rule data as the current starting bit, based on the encoding conditions satisfied by the rule data of a first number of consecutive bits starting from the current starting bit, converting the rule data of a first number of consecutive bits into a check block; the first number is less than or equal to the CPU bit width of the electronic device and greater than a preset number, and the preset number is the number of bits used to store the key value in the check block of the mask type;
[0383] The first valid bit is searched from the bits in the target rule data that have not been converted into the check block, the found valid bit is used as the current starting bit, the encoding condition satisfied by the rule data based on the first number of consecutive bits starting from the current starting bit is returned, and the step of converting the first number of consecutive bits of rule data into a check block is performed until all the rule data with valid bits included in the target rule data are converted into check blocks.
[0384] In one implementation, the conversion module 1402 is specifically configured to:
[0385] Determining whether the first number of consecutive bits of regular data meet a non-mask type encoding condition;
[0386] If satisfied, converting the key value in the first number of consecutive bits of rule data into the non-mask type check block;
[0387] If not, the first number is determined to be the preset number, and the key value and mask in the rule data of the continuous first number of bits are converted into a check block of the mask type.
[0388] In one implementation, the non-masked type includes a full data type, a first length type, and a second length type;
[0389] The encoding conditions of the full data type include: the current starting bit is the first bit of a byte included in the target rule data; and in the mask of the target rule data, the values of consecutive CPU bit width bits starting from the current starting bit are all 1, and the first number is the CPU bit width;
[0390] The encoding condition of the first length type includes: the current start bit is the first bit of a byte included in the target rule data; and in the mask of the target rule data, the values of the first number of bits starting from the current start bit are all 1, and the first number is less than the difference between the CPU bit width and the number of bits occupied by the check block type field;
[0391] The encoding condition of the second length type includes: the current start bit is not the first bit of a byte included in the target rule data; and in the mask of the target rule data, the values of the first number of bits starting from the current start bit are all 1, and the first number is less than the difference between the CPU bit width and the number of bits occupied by the check block type field and the number of bits occupied by the start bit field;
[0392] The determining whether the first number of consecutive regular data bits meets a non-mask type encoding condition includes:
[0393] If the regular data of the first number of consecutive bits meets any one of the encoding conditions of the full data type, the first length type and the second length type, it is determined that the encoding condition of the non-mask type is met; otherwise, it is determined that the encoding condition of the non-mask type is not met.
[0394] In one implementation, the conversion module 1402 is specifically configured to:
[0395] If the first number of consecutive digits of regular data meet the encoding condition of the full data type, converting the key value in the first number of consecutive digits of regular data into a check block of the full data type;
[0396] If the first number of consecutive regular data bits meet the encoding condition of the first length type and do not meet the encoding condition of the full data type, converting the key value in the first number of consecutive regular data bits into a check block of the first length type;
[0397] If the first number of consecutive regular data bits meet the encoding condition of the second length type, the first number of consecutive regular data bits are converted into a check block of the second length type.
[0398] In one implementation, the check block of all data types is used to store the key value in the rule data of the continuous first number of bits;
[0399] The lowest three bits of the check block of the first length type are used to store the type code of the first length type, and the bits from the highest bit to the fourth to last bit are used to store: the key value in the rule data of the first number of bits and a 0, and the value of the unoccupied bit is set to 1 by default;
[0400] The highest three bits in the check block of the second length type are used to store the offset of the current start bit within the byte, the lowest three bits are used to store the type code of the second length type, and the remaining bits are used to store in sequence: the key value in the rule data of the continuous first number of bits and a 0, and the unoccupied bits are set to 1 by default;
[0401] The highest three bits in the check block of the mask type are used to store the offset of the current starting bit within the byte, the lowest three bits are used to store the type code of the mask type, and the remaining bits are used to store in sequence: the key value and mask in the rule data of the continuous first number of bits.
[0402] In one implementation, the conversion module 1402 is further configured to:
[0403] If the target rule data includes range-type rule data, and the maximum and minimum values of the range-type rule data are both less than 2 13 , then converting the rule data of the range type into a check block of the first range type;
[0404] If the minimum value of the rule data of the range type is greater than or equal to 2 13 , or the maximum value is greater than or equal to 2 13 , then converting the rule data of the range type into a check block of a second range type and a check block of a third range type;
[0405] The highest 13 bits of the check block of the first range type are used to store the maximum value, the following 13 bits are used to store the minimum value, the following 2 bits are used to store the first subtype code, and the lowest 3 bits are used to store the code of the range type, and the first subtype code is used to indicate that the check block includes a matching range;
[0406] The highest 26 bits of the second range type check block are used to store the minimum value, the following two bits are used to store the second subtype code, and the lowest 3 bits are used to store the code of the range type, and the second subtype code is used to indicate that the check block includes a minimum value of a matching range;
[0407] The highest 26 bits of the third range type check block are used to store the maximum value, the following two bits are used to store the third subtype code, and the lowest 3 bits are used to store the range type code, and the third subtype code is used to indicate that the check block includes a maximum value of a matching range.
[0408] In one implementation, each check block stored in the RAM includes a byte offset field before it, and the byte offset field before each check block is used to indicate the byte offset of the start bit of the rule data in the check block in the target rule data.
[0409] In one implementation, the acquisition module 1401 is specifically configured to:
[0410] Acquire a global rule set and common rules of a plurality of rules included in the global rule set;
[0411] Based on the common rules, pending rules corresponding to each rule included in the global rule set are generated, and each pending rule is used as the rule to be stored; for each bit, if a rule has the same value in the bit as the common rule corresponding to the rule, then the value of the pending rule corresponding to the rule in the bit is a wildcard, otherwise the value of the pending rule corresponding to the rule in the bit is the same as the value of the rule in the bit.
[0412] Based on the same inventive concept, an embodiment of the present application further provides a message processing method, the device is applied to an electronic device, a random access memory RAM is provided in the electronic device, the target rule data in the matching domain of each rule in the specified rule table of the RAM is stored in the form of a check block, the types of the check block include mask type and non-mask type, the rule data in the check block of the mask type includes a key value and a mask, the rule data in the check block of the non-mask type includes a key value with a continuous effective bit, and the effective bit is a bit with a mask value of 1; Fig.15 As shown, the device comprises:
[0413] Extraction module 1501, used for extracting search key values from the message to be processed;
[0414] A determination module 1502 is used to determine the to-be-matched rule in the specified rule table based on the search key value; and for each check block included in the to-be-matched rule, determine the length of the to-be-matched data according to the type of the check block;
[0415] The matching module 1503 is used to extract the data to be matched according to the length of the data to be matched, starting from the same starting position in the search key value according to the starting position of the rule data included in the check block, and match the data to be matched with the rule data included in the check block; if the search key value successfully matches all the check blocks in the rule to be matched, it is determined that the search key value successfully matches the rule to be matched;
[0416] The processing module 1504 is used to process the message to be processed based on the matching result between the search key value and the rule to be matched.
[0417] In one implementation, a linear table is also stored in the RAM, and the linear table includes a plurality of base addresses, and the base addresses are used to locate the block rule table; the determination module 1502 is specifically used to:
[0418] Using bits within a preset range in the search key value as a search index, determining a target base address pointed to by the search index in the linear table;
[0419] Locating a block rule table based on the target base address, and using the located block rule table as a designated rule table;
[0420] The rules included in the specified rule table are used as the rules to be matched.
[0421] In one implementation, the electronic device is further provided with a ternary content addressable memory TCAM, the TCAM stores a common rule table, the RAM stores a main rule table and a block rule table,
[0422] The block rule table stores a plurality of rules having the same common rules.
[0423] The main rule table includes at least two fields: a split position set and a base address. The rules in the block rule table belong to a subtree determined by the split position set. The split position set is used to locate the position of the rule corresponding to the key value in the block rule table. The base address is used to locate the block rule table. The value of the non-wildcard bit in the common rule is the same as the value of the same bit of all rules in the block rule table pointed to by the base address.
[0424] The common rule table stores common rules, and the common rule table has the same index as the main rule table corresponding to the same block rule table;
[0425] The determination module 1502 is specifically configured to:
[0426] Using the search key value to perform a match in the common rule table, obtaining a primary index of a target entry in the main rule table;
[0427] Locate a block rule table based on a base address included in the target table entry pointed to by the primary index, and use the located block rule table as the designated rule table;
[0428] Determine a secondary index of a to-be-matched rule in the specified rule table based on the search key value and a split position set included in the target table entry pointed to by the primary index;
[0429] The rule pointed to by the secondary index is used as the rule to be matched.
[0430] In one implementation, the apparatus further includes a generating module;
[0431] A generation module is used to generate a common rule table, a main rule table, and a block rule table in the following ways:
[0432] Construct a rule decision tree based on the balance bit of each rule in the global rule set, where the balance bit is the bit with the smallest difference between the number of rules with a value of 0 and the number of rules with a value of 1;
[0433] Starting from each leaf node of the rule decision tree, traverse upward in breadth-first order to obtain the deepest rule subtree corresponding to each leaf node, wherein the deepest rule subtree is the deepest rule subtree among the rule subtrees whose number of balance bits is less than or equal to the preset splitting bit depth;
[0434] Based on each deepest rule subtree, the common rule table, the main rule table and the block rule table are generated.
[0435] In one implementation, the generating module is specifically used for:
[0436] Based on the rule sets included in each deepest rule subtree, a block rule table is generated respectively;
[0437] Based on the balance bits included in each deepest rule subtree, a split position set is generated respectively;
[0438] Generate the main rule table based on the split position set corresponding to each deepest rule subtree and the base address of the block rule table;
[0439] The common rule table is generated based on the table entry index in the main rule table corresponding to each deepest rule subtree and the common rules of the rule set included in each deepest rule subtree.
[0440] In one implementation, the generating module is specifically used for:
[0441] Using a global rule set as a rule set to be split, wherein the global rule set is included in a root node of a rule decision tree;
[0442] Determine the balance position of each rule in the rule set to be split;
[0443] Add the rule whose balance bit value is 0 in the to-be-split rule set to the first side child node, and add the rule whose balance bit value is 1 in the to-be-split rule set to the second side child node;
[0444] For each child node obtained by splitting, if the child node includes multiple rules, the rule set included in the child node is used as the rule set to be split, and the step of determining the balance position of the rules in the rule set to be split is re-executed until each child node obtained by splitting includes one rule, thereby obtaining the rule decision tree.
[0445] In one implementation, the processing module 1504 is specifically configured to:
[0446] If there is one rule to be matched, and the search key value successfully matches the rule to be matched, the message to be processed is processed according to the action field corresponding to the rule to be matched;
[0447] If there are multiple rules to be matched, the message to be processed is processed according to the action field corresponding to a rule to be matched that successfully matches the search key value.
[0448] In one implementation, the determination module 1502 is specifically configured to:
[0449] If the type of the check block is the mask type, determining that the length of the data to be matched is the length of the preset number of bits;
[0450] If the type of the check block is the full data type, determining that the length of the data to be matched is the CPU bit width of the electronic device;
[0451] If the type of the check block is the first length type or the second length type, search for the first bit whose value is 0 from the lowest bit of the rule data included in the check block toward the highest bit, and determine that the length of the data to be matched is the length from the highest bit to the bit found;
[0452] If the type of the check block is a range type, the length of the data to be matched is determined to be a fixed length of the range type field.
[0453] In one implementation, the matching module 1503 is specifically configured to:
[0454] If the type of the check block is the mask type, the key value in the check block and the mask are ANDed, and the result obtained after the ANDed operation is compared with the data to be matched. If they are completely consistent, the match is successful;
[0455] If the type of the check block is the full data type, the data to be matched is compared with all the rule data in the check block. If they are completely consistent, the match is successful;
[0456] If the type of the check block is the first length type or the second length type, starting from the highest bit of the rule data included in the check block, extract the rule data according to the length of the data to be matched, and compare the extracted rule data with the data to be matched. If they are completely consistent, the match is successful;
[0457] If the type of the check block is a range type, a matching range is obtained from the check block, and if the data to be matched belongs to the matching range, the match is successful.
[0458] The embodiment of the present invention further provides an electronic device, such as Fig.16 As shown, it includes a processor 1601, a communication interface 1602, a memory 1603 and a communication bus 1604, wherein the processor 1601, the communication interface 1602, and the memory 1603 communicate with each other through the communication bus 1604.
[0459] Memory 1603, used for storing computer programs;
[0460] The processor 1601 is used to implement the steps of the above-mentioned rule storage method or message processing method when executing the program stored in the memory 1603.
[0461] The communication bus mentioned in the above electronic device can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.
[0462] The communication interface is used for communication between the above electronic device and other devices.
[0463] The memory may include a random access memory (RAM) or a non-volatile memory (NVM), such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0464] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0465] In another embodiment of the present invention, a computer-readable storage medium is provided, in which a computer program is stored. When the computer program is executed by a processor, the steps of the above-mentioned rule storage method or message processing method are implemented.
[0466] In another embodiment of the present invention, a computer program product including instructions is provided, which, when executed on a computer, enables the computer to execute the steps of the rule storage method or the message processing method in the above embodiment.
[0467] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented by software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present invention is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a website site, computer, server or data center to another website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk Solid State Disk (SSD)), etc.
[0468] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0469] Each embodiment in this specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0470] The above description is only a preferred embodiment of the present invention and is not intended to limit the protection scope of the present invention. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention are included in the protection scope of the present invention.
Claims
1. A rule storage method, characterized in that: The method is applied to an electronic device, wherein the electronic device is provided with a RAM, and the method comprises: Acquire a rule to be stored, wherein the rule to be stored includes a matching domain, wherein the matching domain includes target rule data, and the target rule data is a key value and a mask in the matching domain; converting the target rule data into at least one check block, and storing the target rule data in the RAM in the form of the check block; The types of the check blocks include mask type and non-mask type, the rule data in the check blocks of the mask type include a key value and a mask, the rule data in the check blocks of the non-mask type include a key value with continuous validity bits, excluding a mask, and the validity bit is a bit with a mask value of 1; The converting the target rule data into at least one check block comprises: Taking the first valid bit of the target rule data as the current starting bit, based on the encoding conditions satisfied by the rule data of a first number of consecutive bits starting from the current starting bit, converting the rule data of a first number of consecutive bits into a check block; the first number is less than or equal to the CPU bit width of the electronic device and greater than a preset number, and the preset number is the number of bits used to store the key value in the check block of the mask type; The first valid bit is searched from the bits in the target rule data that have not been converted into the check block, the found valid bit is used as the current starting bit, the encoding condition satisfied by the rule data based on the first number of consecutive bits starting from the current starting bit is returned, and the step of converting the first number of consecutive bits of rule data into a check block is performed until all the rule data with valid bits included in the target rule data are converted into check blocks.
2. The method according to claim 1, characterized in that The step of converting the first number of consecutive regular data bits starting from the current start bit into a check block based on the encoding condition satisfied by the first number of consecutive regular data bits starting from the current start bit comprises: Determining whether the first number of consecutive bits of regular data meet a non-mask type encoding condition; If satisfied, converting the key value in the first number of consecutive bits of rule data into the non-mask type check block; If not, the first number is determined to be the preset number, and the key value and mask in the rule data of the continuous first number of bits are converted into a check block of the mask type.
3. The method according to claim 2, characterized in that The non-mask type includes a full data type, a first length type, and a second length type; The encoding conditions of the full data type include: the current starting bit is the first bit of a byte included in the target rule data; and in the mask of the target rule data, the values of consecutive CPU bit width bits starting from the current starting bit are all 1, and the first number is the CPU bit width; The encoding condition of the first length type includes: the current start bit is the first bit of a byte included in the target rule data; and in the mask of the target rule data, the values of the first number of bits starting from the current start bit are all 1, and the first number is less than the difference between the CPU bit width and the number of bits occupied by the check block type field; The encoding condition of the second length type includes: the current start bit is not the first bit of a byte included in the target rule data; and in the mask of the target rule data, the values of the first number of bits starting from the current start bit are all 1, and the first number is less than the difference between the CPU bit width and the number of bits occupied by the check block type field and the number of bits occupied by the start bit field; The determining whether the first number of consecutive regular data bits meets a non-mask type encoding condition includes: If the regular data of the first number of consecutive bits meets any one of the encoding conditions of the full data type, the first length type and the second length type, it is determined that the encoding condition of the non-mask type is met; otherwise, it is determined that the encoding condition of the non-mask type is not met.
4. The method according to claim 3, characterized in that The step of converting the key value in the first number of consecutive bits of regular data into the non-mask type check block comprises: If the first number of consecutive digits of regular data meet the encoding condition of the full data type, converting the key value in the first number of consecutive digits of regular data into a check block of the full data type; If the first number of consecutive regular data bits meet the encoding condition of the first length type and do not meet the encoding condition of the full data type, converting the key value in the first number of consecutive regular data bits into a check block of the first length type; If the first number of consecutive regular data bits meet the encoding condition of the second length type, the first number of consecutive regular data bits are converted into a check block of the second length type.
5. The method according to claim 4, characterized in that The check block of all data types is used to store the key value in the rule data of the continuous first number of bits; The lowest three bits of the check block of the first length type are used to store the type code of the first length type, and the bits from the highest bit to the fourth to last bit are used to store: the key value in the rule data of the first number of bits and a 0, and the value of the unoccupied bit is set to 1 by default; The highest three bits in the check block of the second length type are used to store the offset of the current start bit within the byte, the lowest three bits are used to store the type code of the second length type, and the remaining bits are used to store in sequence: the key value in the rule data of the continuous first number of bits and a 0, and the unoccupied bits are set to 1 by default; The highest three bits in the check block of the mask type are used to store the offset of the current starting bit within the byte, the lowest three bits are used to store the type code of the mask type, and the remaining bits are used to store in sequence: the key value and mask in the rule data of the continuous first number of bits.
6. The method according to any one of claims 1 to 5, characterized in that: The method further comprises: If the target rule data includes range-type rule data, and the maximum and minimum values of the range-type rule data are both less than 2 13 , then converting the rule data of the range type into a check block of the first range type; If the minimum value of the rule data of the range type is greater than or equal to 2 13 , or the maximum value is greater than or equal to 2 13 , then converting the rule data of the range type into a check block of the second range type and a check block of the third range type; The highest 13 bits of the check block of the first range type are used to store the maximum value, the following 13 bits are used to store the minimum value, the following 2 bits are used to store the first subtype code, and the lowest 3 bits are used to store the code of the range type, and the first subtype code is used to indicate that the check block includes a matching range; The highest 26 bits of the second range type check block are used to store the minimum value, the following two bits are used to store the second subtype code, and the lowest 3 bits are used to store the code of the range type, and the second subtype code is used to indicate that the check block includes a minimum value of a matching range; The highest 26 bits of the third range type check block are used to store the maximum value, the following two bits are used to store the third subtype code, and the lowest 3 bits are used to store the range type code, and the third subtype code is used to indicate that the check block includes a maximum value of a matching range.
7. The method according to claim 6, characterized in that Each check block stored in the RAM includes a byte offset field before it. The byte offset field before each check block is used to indicate the byte offset of the start bit of the rule data in the check block in the target rule data.
8. The method according to claim 1, characterized in that The obtaining of the rules to be stored includes: Acquire a global rule set and common rules of a plurality of rules included in the global rule set; Based on the common rules, pending rules corresponding to each rule included in the global rule set are generated, and each pending rule is used as the rule to be stored; for each bit, if a rule has the same value in the bit as the common rule corresponding to the rule, then the value of the pending rule corresponding to the rule in the bit is a wildcard, otherwise the value of the pending rule corresponding to the rule in the bit is the same as the value of the rule in the bit.
9. A message processing method, characterized in that: The method is applied to an electronic device, wherein a random access memory RAM is provided in the electronic device, and target rule data in a matching domain of each rule in a specified rule table of the RAM is stored in the form of a check block, wherein the types of the check block include a mask type and a non-mask type, wherein the rule data in the check block of the mask type includes a key value and a mask, and the rule data in the check block of the non-mask type includes a key value with continuous effective bits but does not include a mask, wherein the effective bit is a bit with a mask value of 1; the method is characterized in that: Extract and search key values from the message to be processed; Determine the to-be-matched rule in the specified rule table based on the search key value; For each check block included in the to-be-matched rule, determining the length of the to-be-matched data according to the type of the check block; According to the starting position of the rule data included in the check block, starting from the same starting position in the search key value, extracting the data to be matched according to the length of the data to be matched, and matching the data to be matched with the rule data included in the check block; If the search key value successfully matches all the check blocks in the to-be-matched rule, it is determined that the search key value successfully matches the to-be-matched rule; Based on the matching result between the search key value and the rule to be matched, the message to be processed is processed.
10. The method according to claim 9, characterized in that The RAM also stores a linear table, which includes a plurality of base addresses, and the base addresses are used to locate the block rule table; the step of determining the to-be-matched rule in the specified rule table based on the search key value includes: Using bits within a preset range in the search key value as a search index, and determining a target base address pointed to by the search index in the linear table; Locating a block rule table based on the target base address, and using the located block rule table as the designated rule table; The rules included in the specified rule table are used as rules to be matched.
11. The method according to claim 9, characterized in that The electronic device is also provided with a ternary content addressable memory TCAM, the TCAM stores a common rule table, and the RAM stores a main rule table and a block rule table: The block rule table stores a plurality of rules having the same common rule; The main rule table includes at least two fields: a split position set and a base address. The rules in the block rule table belong to a subtree determined by the split position set. The split position set is used to locate the position of the rule corresponding to the key value in the block rule table. The base address is used to locate the block rule table. The value of the non-wildcard bit in the common rule is the same as the value of the same bit of all rules in the block rule table pointed to by the base address. The common rule table stores common rules, and the common rule table has the same index as the main rule table corresponding to the same block rule table; The determining the to-be-matched rule in the specified rule table based on the search key value includes: Using the search key value to perform a match in the common rule table, obtaining a primary index of a target entry in the main rule table; Locate a block rule table based on a base address included in the target table entry pointed to by the primary index, and use the located block rule table as the designated rule table; Determine a secondary index of a to-be-matched rule in the specified rule table based on the search key value and a split position set included in the target table entry pointed to by the primary index; The rule pointed to by the secondary index is used as the rule to be matched.
12. The method according to claim 11, characterized in that The common rule table, main rule table and block rule table are generated in the following manner: Construct a rule decision tree based on the balance bit of each rule in the global rule set, where the balance bit is the bit with the smallest difference between the number of rules with a value of 0 and the number of rules with a value of 1; Starting from each leaf node of the rule decision tree, traverse upward in breadth-first order to obtain the deepest rule subtree corresponding to each leaf node, wherein the deepest rule subtree is the deepest rule subtree among the rule subtrees whose number of balance bits is less than or equal to the preset splitting bit depth; Based on each deepest rule subtree, the common rule table, the main rule table and the block rule table are generated.
13. The method according to claim 12, characterized in that The generating the common rule table, the main rule table and the block rule table based on each deepest rule subtree comprises: Based on the rule sets included in each deepest rule subtree, a block rule table is generated respectively; Based on the balance bits included in each deepest rule subtree, a split position set is generated respectively; Generate the main rule table based on the split position set corresponding to each deepest rule subtree and the base address of the block rule table; The common rule table is generated based on the table entry index in the main rule table corresponding to each deepest rule subtree and the common rules of the rule set included in each deepest rule subtree.
14. The method according to claim 12, characterized in that The step of constructing a rule decision tree based on the balance position of each rule in the global rule set includes: Using a global rule set as a rule set to be split, wherein the global rule set is included in a root node of a rule decision tree; Determine the balance position of each rule in the rule set to be split; Add the rule whose balance bit value is 0 in the to-be-split rule set to the first side child node, and add the rule whose balance bit value is 1 in the to-be-split rule set to the second side child node; For each child node obtained by splitting, if the child node includes multiple rules, the rule set included in the child node is used as the rule set to be split, and the step of determining the balance position of each rule in the rule set to be split is re-executed until each child node obtained by splitting includes one rule, thereby obtaining the rule decision tree.
15. The method according to any one of claims 9 to 11, characterized in that: The processing of the message to be processed based on the matching result between the search key value and the rule to be matched includes: If there is one rule to be matched, and the search key value successfully matches the rule to be matched, the message to be processed is processed according to the action field corresponding to the rule to be matched; If there are multiple rules to be matched, the message to be processed is processed according to the action field corresponding to a rule to be matched that successfully matches the search key value.
16. The method according to claim 9, characterized in that The determining the length of the data to be matched according to the type of the check block includes: If the type of the check block is the mask type, determining that the length of the data to be matched is a length of a preset number of bits; the preset number is the number of bits used to store the key value in the check block of the mask type; The non-mask type includes a full data type, a first length type, and a second length type; If the type of the check block is the full data type, determining that the length of the data to be matched is the CPU bit width of the electronic device; If the type of the check block is the first length type or the second length type, search for the first bit whose value is 0 from the lowest bit of the rule data included in the check block toward the highest bit, and determine that the length of the data to be matched is the length from the highest bit to the bit found; If the type of the check block is a range type, the length of the data to be matched is determined to be a fixed length of the range type field.
17. The method according to claim 9, characterized in that The matching of the to-be-matched data with the rule data included in the verification block includes: If the type of the check block is the mask type, the key value in the check block and the mask are ANDed, and the result obtained after the ANDed operation is compared with the data to be matched. If they are completely consistent, the match is successful; The non-mask type includes a full data type, a first length type, and a second length type; If the type of the check block is the full data type, the data to be matched is compared with all the rule data in the check block. If they are completely consistent, the match is successful; If the type of the check block is the first length type or the second length type, starting from the highest bit of the rule data included in the check block, extract the rule data according to the length of the data to be matched, and compare the extracted rule data with the data to be matched. If they are completely consistent, the match is successful; If the type of the check block is a range type, a matching range is obtained from the check block, and if the data to be matched belongs to the matching range, the match is successful.
18. A rule storage device, characterized in that: The device is applied to an electronic device, wherein the electronic device is provided with a RAM, and the device comprises: An acquisition module, used for acquiring rules to be stored, wherein the rules to be stored include a matching domain, wherein the matching domain includes target rule data, and the target rule data is a key value and a mask in the matching domain; A conversion module, used for converting the target rule data into at least one check block; A storage module, used for storing the target rule data in the RAM in the form of a check block; The types of the check blocks include mask type and non-mask type, the rule data in the check blocks of the mask type include a key value and a mask, the rule data in the check blocks of the non-mask type include a key value with continuous validity bits, excluding a mask, and the validity bit is a bit with a mask value of 1; The conversion module is specifically used for: Taking the first valid bit of the target rule data as the current starting bit, based on the encoding conditions satisfied by the rule data of a first number of consecutive bits starting from the current starting bit, converting the rule data of a first number of consecutive bits into a check block; the first number is less than or equal to the CPU bit width of the electronic device and greater than a preset number, and the preset number is the number of bits used to store the key value in the check block of the mask type; The first valid bit is searched from the bits in the target rule data that have not been converted into the check block, the found valid bit is used as the current starting bit, the encoding condition satisfied by the rule data based on the first number of consecutive bits starting from the current starting bit is returned, and the step of converting the first number of consecutive bits of rule data into a check block is performed until all the rule data with valid bits included in the target rule data are converted into check blocks.
19. The device according to claim 18, characterized in that The conversion module is further used for: If the target rule data includes range-type rule data, and the maximum and minimum values of the range-type rule data are both less than 2 13 , then converting the rule data of the range type into a check block of the first range type; If the minimum value of the rule data of the range type is greater than or equal to 2 13 , or the maximum value is greater than or equal to 2 13 , then converting the rule data of the range type into a check block of the second range type and a check block of the third range type; The highest 13 bits of the check block of the first range type are used to store the maximum value, the following 13 bits are used to store the minimum value, the following 2 bits are used to store the first subtype code, and the lowest 3 bits are used to store the code of the range type, and the first subtype code is used to indicate that the check block includes a matching range; The highest 26 bits of the second range type check block are used to store the minimum value, the following two bits are used to store the second subtype code, and the lowest 3 bits are used to store the code of the range type, and the second subtype code is used to indicate that the check block includes a minimum value of a matching range; The highest 26 bits of the third range type check block are used to store the maximum value, the following two bits are used to store the third subtype code, and the lowest 3 bits are used to store the range type code, and the third subtype code is used to indicate that the check block includes a maximum value of a matching range.
20. The device according to claim 18, characterized in that Each check block stored in the RAM includes a byte offset field before it. The byte offset field before each check block is used to indicate the byte offset of the start bit of the rule data in the check block in the target rule data.
21. A message processing device, characterized in that: The device is applied to an electronic device, wherein a random access memory RAM is provided in the electronic device, and the target rule data in the matching domain of each rule in the designated rule table of the RAM is stored in the form of a check block, wherein the types of the check block include a mask type and a non-mask type, wherein the rule data in the check block of the mask type includes a key value and a mask, and the rule data in the check block of the non-mask type includes a key value with continuous effective bits, but does not include a mask, and the effective bit is a bit with a mask value of 1; the device is characterized in that: An extraction module is used to extract and search key values from the message to be processed; A determination module, configured to determine the to-be-matched rule in the specified rule table based on the search key value; and for each check block included in the to-be-matched rule, determine the length of the to-be-matched data according to the type of the check block; A matching module, configured to extract the data to be matched according to the length of the data to be matched, starting from the same starting position in the search key value according to the starting position of the rule data included in the check block, and match the data to be matched with the rule data included in the check block; if the search key value successfully matches all the check blocks in the rule to be matched, it is determined that the search key value successfully matches the rule to be matched; A processing module is used to process the message to be processed based on the matching result between the search key value and the rule to be matched.
22. The device according to claim 21, characterized in that The RAM also stores a linear table, which includes a plurality of base addresses, and the base addresses are used to locate the block rule table; the determination module is specifically used to: Using bits within a preset range in the search key value as a search index, and determining a target base address pointed to by the search index in the linear table; Locating a block rule table based on the target base address, and using the located block rule table as the designated rule table; The rules included in the specified rule table are used as rules to be matched.
23. The device according to claim 21, characterized in that The electronic device is also provided with a ternary content addressable memory TCAM, the TCAM stores a common rule table, and the RAM stores a main rule table and a block rule table: The block rule table stores a plurality of rules having the same common rule; The main rule table includes at least two fields: a split position set and a base address. The rules in the block rule table belong to a subtree determined by the split position set. The split position set is used to locate the position of the rule corresponding to the key value in the block rule table. The base address is used to locate the block rule table. The value of the non-wildcard bit in the common rule is the same as the value of the same bit of all rules in the block rule table pointed to by the base address. The common rule table stores common rules, and the common rule table has the same index as the main rule table corresponding to the same block rule table; The determination module is specifically used for: Using the search key value to perform a match in the common rule table, obtaining a primary index of a target entry in the main rule table; Locate a block rule table based on a base address included in the target table entry pointed to by the primary index, and use the located block rule table as the designated rule table; Determine a secondary index of a to-be-matched rule in the specified rule table based on the search key value and a split position set included in the target table entry pointed to by the primary index; The rule pointed to by the secondary index is used as the rule to be matched.
24. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, for implementing the method steps described in any one of claims 1-8 or 9-17 when executing a program stored in a memory.
25. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method steps described in any one of claims 1-8 or 9-17 are implemented.
Citation Information
Patent Citations
Efficient matching of TCAM rules using hash tables in RAM
CN107800631A
Mask data query method, apparatus and device
CN112307070A
ACL rule classification method, lookup method and device
CN112425131A
Decision tree adjusting method and device
CN115037695A
Verification of a RAM-based TCAM
US10115463B1
