1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (C) 2020 Marvell International Ltd.
3 */
4
5 #include <rte_malloc.h>
6 #include <rte_regexdev.h>
7
8 #include "cn9k_regexdev.h"
9 #include "cn9k_regexdev_compiler.h"
10
11 #ifdef REE_COMPILER_SDK
12 #include <rxp-compiler.h>
13
14 static int
ree_rule_db_compile(const struct rte_regexdev_rule * rules,uint16_t nb_rules,struct rxp_rof ** rof,struct rxp_rof ** rofi,struct rxp_rof * rof_for_incremental_compile,struct rxp_rof * rofi_for_incremental_compile)15 ree_rule_db_compile(const struct rte_regexdev_rule *rules,
16 uint16_t nb_rules, struct rxp_rof **rof, struct rxp_rof **rofi,
17 struct rxp_rof *rof_for_incremental_compile,
18 struct rxp_rof *rofi_for_incremental_compile)
19 {
20 /*INPUT*/
21 struct rxp_prefix_selection_control_list *prefix_selection_control_list
22 = NULL;
23 struct rxp_blacklist_data_sample *blacklist_sample_data = NULL;
24 struct rxp_rule_ids_to_remove *rule_ids_to_remove = NULL;
25 struct rxp_roff *roff_for_incremental_compile = NULL;
26
27 /*OPTIONS - setting default values*/
28 enum rxp_virtual_prefix_mode virtual_prefix_mode =
29 RXP_VIRTUAL_PREFIX_MODE_0;
30 enum rxp_prefix_capacity prefix_capacity = RXP_PREFIX_CAPACITY_32K;
31 /**< rxp_global_regex_options_flags*/
32 enum rxp_compiler_objective objective = RXP_COMPILER_OBJECTIVE_5;
33 enum rxp_tpe_data_width tpe_data_width = RXP_TPE_DATA_WIDTH_4;
34 uint32_t compiler_options = RXP_COMPILER_OPTIONS_FORCE;
35 /**< rxp_compiler_options_flags*/
36 enum rxp_verbose_level verbose = RXP_VERBOSE_LEVEL_3;
37 enum rxp_version set_rxp_version = RXP_VERSION_V5_8;
38 uint32_t compiler_output_flags = 0;
39 /**< rxp_compiler_output_flags*/
40 uint32_t global_regex_options = 0;
41 /**< rxp_global_regex_options_flags*/
42 float set_auto_blacklist = 0;
43 uint32_t max_rep_max = 65535;
44 uint32_t divide_ruleset = 1;
45 struct rxp_ruleset ruleset;
46 float ptpb_threshold = 0;
47 uint32_t set_max = 0;
48 uint32_t threads = 1;
49
50 /*OUTPUT*/
51 struct rxp_rule_direction_analysis *rule_direction_analysis = NULL;
52 struct rxp_compilation_statistics *compilation_statistics = NULL;
53 struct rxp_prefix_selection_control_list *generated_pscl = NULL;
54 struct rxp_uncompiled_rules_log *uncompiled_rules_log = NULL;
55 struct rxp_critical_rules_rank *critical_rules_rank = NULL;
56 struct rxp_compiled_rules_log *compiled_rules_log = NULL;
57 struct rxp_roff *roff = NULL;
58
59 uint16_t i;
60 int ret;
61
62 ruleset.number_of_entries = nb_rules;
63 ruleset.rules = rte_malloc("rxp_rule_entry",
64 nb_rules*sizeof(struct rxp_rule_entry), 0);
65
66 if (ruleset.rules == NULL) {
67 cn9k_err("Could not allocate memory for rule compilation\n");
68 return -EFAULT;
69 }
70 if (rof_for_incremental_compile)
71 compiler_options |= RXP_COMPILER_OPTIONS_INCREMENTAL;
72 if (rofi_for_incremental_compile)
73 compiler_options |= RXP_COMPILER_OPTIONS_CHECKSUM;
74
75 for (i = 0; i < nb_rules; i++) {
76 ruleset.rules[i].number_of_prefix_entries = 0;
77 ruleset.rules[i].prefix = NULL;
78 ruleset.rules[i].rule = rules[i].pcre_rule;
79 ruleset.rules[i].rule_id = rules[i].rule_id;
80 ruleset.rules[i].subset_id = rules[i].group_id;
81 ruleset.rules[i].rule_direction_type =
82 RXP_RULE_DIRECTION_TYPE_NONE;
83 }
84
85 ret = rxp_compile_advanced(
86 /*INPUT*/
87 &ruleset,
88 prefix_selection_control_list,
89 rof_for_incremental_compile,
90 roff_for_incremental_compile,
91 rofi_for_incremental_compile,
92 rule_ids_to_remove,
93 blacklist_sample_data,
94
95 /*OPTIONS*/
96 compiler_options,
97 prefix_capacity,
98 global_regex_options,
99 set_auto_blacklist,
100 set_max,
101 objective,
102 ptpb_threshold,
103 max_rep_max,
104 threads,
105 set_rxp_version,
106 verbose,
107 tpe_data_width,
108 virtual_prefix_mode,
109 compiler_output_flags,
110 divide_ruleset,
111
112 /*OUTPUT*/
113 &compilation_statistics,
114 &compiled_rules_log,
115 &critical_rules_rank,
116 &rule_direction_analysis,
117 &uncompiled_rules_log,
118 rof,
119 &roff,
120 rofi,
121 &generated_pscl);
122 rte_free(ruleset.rules);
123
124 return ret;
125 }
126
127 int
cn9k_ree_rule_db_compile_prog(struct rte_regexdev * dev)128 cn9k_ree_rule_db_compile_prog(struct rte_regexdev *dev)
129 {
130 struct cn9k_ree_data *data = dev->data->dev_private;
131 struct roc_ree_vf *vf = &data->vf;
132 char compiler_version[] = "20.5.2.eda0fa2";
133 char timestamp[] = "19700101_000001";
134 uint32_t rule_db_len, rule_dbi_len;
135 struct rxp_rof *rofi_inc_p = NULL;
136 struct rxp_rof_entry rule_dbi[6];
137 char *rofi_rof_entries = NULL;
138 struct rxp_rof *rofi = NULL;
139 struct rxp_rof *rof = NULL;
140 struct rxp_rof rofi_inc;
141 struct rxp_rof rof_inc;
142 char *rule_db = NULL;
143 int ret;
144
145 ree_func_trace();
146
147 ret = roc_ree_rule_db_len_get(vf, &rule_db_len, &rule_dbi_len);
148 if (ret != 0) {
149 cn9k_err("Could not get rule db length");
150 return ret;
151 }
152
153 if (rule_db_len > 0) {
154 cn9k_ree_dbg("Incremental compile, rule db len %d rule dbi len %d",
155 rule_db_len, rule_dbi_len);
156 rule_db = rte_malloc("ree_rule_db", rule_db_len, 0);
157 if (!rule_db) {
158 cn9k_err("Could not allocate memory for rule db");
159 return -EFAULT;
160 }
161
162 ret = roc_ree_rule_db_get(vf, rule_db, rule_db_len,
163 (char *)rule_dbi, rule_dbi_len);
164 if (ret) {
165 cn9k_err("Could not read rule db");
166 rte_free(rule_db);
167 return -EFAULT;
168 }
169 rof_inc.rof_revision = 0;
170 rof_inc.rof_version = 2;
171 rof_inc.rof_entries = (struct rxp_rof_entry *)rule_db;
172 rof_inc.rxp_compiler_version = compiler_version;
173 rof_inc.timestamp = timestamp;
174 rof_inc.number_of_entries =
175 (rule_db_len/sizeof(struct rxp_rof_entry));
176
177 if (rule_dbi_len > 0) {
178 /* incremental compilation not the first time */
179 rofi_inc.rof_revision = 0;
180 rofi_inc.rof_version = 2;
181 rofi_inc.rof_entries = rule_dbi;
182 rofi_inc.rxp_compiler_version = compiler_version;
183 rofi_inc.timestamp = timestamp;
184 rofi_inc.number_of_entries =
185 (rule_dbi_len/sizeof(struct rxp_rof_entry));
186 rofi_inc_p = &rofi_inc;
187 }
188 ret = ree_rule_db_compile(data->rules, data->nb_rules, &rof,
189 &rofi, &rof_inc, rofi_inc_p);
190 if (rofi->number_of_entries == 0) {
191 cn9k_ree_dbg("No change to rule db");
192 ret = 0;
193 goto free_structs;
194 }
195 rule_dbi_len = rofi->number_of_entries *
196 sizeof(struct rxp_rof_entry);
197 rofi_rof_entries = (char *)rofi->rof_entries;
198 } else {
199 /* full compilation */
200 ret = ree_rule_db_compile(data->rules, data->nb_rules, &rof,
201 &rofi, NULL, NULL);
202 }
203 if (ret != 0) {
204 cn9k_err("Could not compile rule db");
205 goto free_structs;
206 }
207 rule_db_len = rof->number_of_entries * sizeof(struct rxp_rof_entry);
208 ret = roc_ree_rule_db_prog(vf, (char *)rof->rof_entries, rule_db_len,
209 rofi_rof_entries, rule_dbi_len);
210 if (ret)
211 cn9k_err("Could not program rule db");
212
213 free_structs:
214 rxp_free_structs(NULL, NULL, NULL, NULL, NULL, &rof, NULL, &rofi, NULL,
215 1);
216
217 rte_free(rule_db);
218
219 return ret;
220 }
221 #else
222 int
cn9k_ree_rule_db_compile_prog(struct rte_regexdev * dev)223 cn9k_ree_rule_db_compile_prog(struct rte_regexdev *dev)
224 {
225 RTE_SET_USED(dev);
226 return -ENOTSUP;
227 }
228 #endif
229