4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <netinet/ip.h>
43 #define MAX_ACL_RULE_NUM 1000
46 * Rule and trace formats definitions.
58 * That effectively defines order of IPV4 classifications:
62 * - PORTS (SRC and DST)
72 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
74 .type = RTE_ACL_FIELD_TYPE_BITMASK,
75 .size = sizeof(uint8_t),
76 .field_index = PROTO_FIELD_IPV4,
77 .input_index = RTE_ACL_IPV4_PROTO,
81 .type = RTE_ACL_FIELD_TYPE_MASK,
82 .size = sizeof(uint32_t),
83 .field_index = SRC_FIELD_IPV4,
84 .input_index = RTE_ACL_IPV4_SRC,
85 .offset = offsetof(struct ip, ip_src) - offsetof(struct ip, ip_p)
88 .type = RTE_ACL_FIELD_TYPE_MASK,
89 .size = sizeof(uint32_t),
90 .field_index = DST_FIELD_IPV4,
91 .input_index = RTE_ACL_IPV4_DST,
92 .offset = offsetof(struct ip, ip_dst) - offsetof(struct ip, ip_p)
95 .type = RTE_ACL_FIELD_TYPE_RANGE,
96 .size = sizeof(uint16_t),
97 .field_index = SRCP_FIELD_IPV4,
98 .input_index = RTE_ACL_IPV4_PORTS,
99 .offset = sizeof(struct ip) - offsetof(struct ip, ip_p)
102 .type = RTE_ACL_FIELD_TYPE_RANGE,
103 .size = sizeof(uint16_t),
104 .field_index = DSTP_FIELD_IPV4,
105 .input_index = RTE_ACL_IPV4_PORTS,
106 .offset = sizeof(struct ip) - offsetof(struct ip, ip_p) +
111 RTE_ACL_RULE_DEF(acl4_rules, RTE_DIM(ipv4_defs));
113 const struct acl4_rules acl4_rules_in[] = {
115 .data = {.userdata = PROTECT(5), .category_mask = 1, .priority = 1},
116 /* destination IPv4 */
117 .field[2] = {.value.u32 = IPv4(192, 168, 105, 0),
118 .mask_range.u32 = 24,},
120 .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
121 /* destination port */
122 .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
125 .data = {.userdata = PROTECT(6), .category_mask = 1, .priority = 2},
126 /* destination IPv4 */
127 .field[2] = {.value.u32 = IPv4(192, 168, 106, 0),
128 .mask_range.u32 = 24,},
130 .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
131 /* destination port */
132 .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
135 .data = {.userdata = PROTECT(7), .category_mask = 1, .priority = 3},
136 /* destination IPv4 */
137 .field[2] = {.value.u32 = IPv4(192, 168, 107, 0),
138 .mask_range.u32 = 24,},
140 .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
141 /* destination port */
142 .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
145 .data = {.userdata = PROTECT(8), .category_mask = 1, .priority = 4},
146 /* destination IPv4 */
147 .field[2] = {.value.u32 = IPv4(192, 168, 108, 0),
148 .mask_range.u32 = 24,},
150 .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
151 /* destination port */
152 .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
155 .data = {.userdata = PROTECT(9), .category_mask = 1, .priority = 5},
156 /* destination IPv4 */
157 .field[2] = {.value.u32 = IPv4(192, 168, 200, 0),
158 .mask_range.u32 = 24,},
160 .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
161 /* destination port */
162 .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
165 .data = {.userdata = BYPASS, .category_mask = 1, .priority = 6},
166 /* destination IPv4 */
167 .field[2] = {.value.u32 = IPv4(192, 168, 250, 0),
168 .mask_range.u32 = 24,},
170 .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
171 /* destination port */
172 .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
176 const struct acl4_rules acl4_rules_out[] = {
178 .data = {.userdata = PROTECT(5), .category_mask = 1, .priority = 1},
179 /* destination IPv4 */
180 .field[2] = {.value.u32 = IPv4(192, 168, 115, 0),
181 .mask_range.u32 = 24,},
183 .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
184 /* destination port */
185 .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
188 .data = {.userdata = PROTECT(6), .category_mask = 1, .priority = 2},
189 /* destination IPv4 */
190 .field[2] = {.value.u32 = IPv4(192, 168, 116, 0),
191 .mask_range.u32 = 24,},
193 .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
194 /* destination port */
195 .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
198 .data = {.userdata = PROTECT(7), .category_mask = 1, .priority = 3},
199 /* destination IPv4 */
200 .field[2] = {.value.u32 = IPv4(192, 168, 117, 0),
201 .mask_range.u32 = 24,},
203 .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
204 /* destination port */
205 .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
208 .data = {.userdata = PROTECT(8), .category_mask = 1, .priority = 4},
209 /* destination IPv4 */
210 .field[2] = {.value.u32 = IPv4(192, 168, 118, 0),
211 .mask_range.u32 = 24,},
213 .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
214 /* destination port */
215 .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
218 .data = {.userdata = PROTECT(9), .category_mask = 1, .priority = 5},
219 /* destination IPv4 */
220 .field[2] = {.value.u32 = IPv4(192, 168, 210, 0),
221 .mask_range.u32 = 24,},
223 .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
224 /* destination port */
225 .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
228 .data = {.userdata = BYPASS, .category_mask = 1, .priority = 6},
229 /* destination IPv4 */
230 .field[2] = {.value.u32 = IPv4(192, 168, 240, 0),
231 .mask_range.u32 = 24,},
233 .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
234 /* destination port */
235 .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
240 print_one_ipv4_rule(const struct acl4_rules *rule, int extra)
242 unsigned char a, b, c, d;
244 uint32_t_to_char(rule->field[SRC_FIELD_IPV4].value.u32,
246 printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
247 rule->field[SRC_FIELD_IPV4].mask_range.u32);
248 uint32_t_to_char(rule->field[DST_FIELD_IPV4].value.u32,
250 printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
251 rule->field[DST_FIELD_IPV4].mask_range.u32);
252 printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
253 rule->field[SRCP_FIELD_IPV4].value.u16,
254 rule->field[SRCP_FIELD_IPV4].mask_range.u16,
255 rule->field[DSTP_FIELD_IPV4].value.u16,
256 rule->field[DSTP_FIELD_IPV4].mask_range.u16,
257 rule->field[PROTO_FIELD_IPV4].value.u8,
258 rule->field[PROTO_FIELD_IPV4].mask_range.u8);
260 printf("0x%x-0x%x-0x%x ",
261 rule->data.category_mask,
263 rule->data.userdata);
267 dump_ipv4_rules(const struct acl4_rules *rule, int num, int extra)
271 for (i = 0; i < num; i++, rule++) {
272 printf("\t%d:", i + 1);
273 print_one_ipv4_rule(rule, extra);
278 static struct rte_acl_ctx *
279 acl4_init(const char *name, int socketid, const struct acl4_rules *rules,
283 struct rte_acl_param acl_param;
284 struct rte_acl_config acl_build_param;
285 struct rte_acl_ctx *ctx;
287 printf("Creating SP context with %u max rules\n", MAX_ACL_RULE_NUM);
289 memset(&acl_param, 0, sizeof(acl_param));
291 /* Create ACL contexts */
292 snprintf(s, sizeof(s), "%s_%d", name, socketid);
294 printf("IPv4 %s entries [%u]:\n", s, rules_nb);
295 dump_ipv4_rules(rules, rules_nb, 1);
298 acl_param.socket_id = socketid;
299 acl_param.rule_size = RTE_ACL_RULE_SZ(RTE_DIM(ipv4_defs));
300 acl_param.max_rule_num = MAX_ACL_RULE_NUM;
302 ctx = rte_acl_create(&acl_param);
304 rte_exit(EXIT_FAILURE, "Failed to create ACL context\n");
306 if (rte_acl_add_rules(ctx, (const struct rte_acl_rule *)rules,
308 rte_exit(EXIT_FAILURE, "add rules failed\n");
311 memset(&acl_build_param, 0, sizeof(acl_build_param));
313 acl_build_param.num_categories = DEFAULT_MAX_CATEGORIES;
314 acl_build_param.num_fields = RTE_DIM(ipv4_defs);
315 memcpy(&acl_build_param.defs, ipv4_defs, sizeof(ipv4_defs));
317 if (rte_acl_build(ctx, &acl_build_param) != 0)
318 rte_exit(EXIT_FAILURE, "Failed to build ACL trie\n");
326 sp_init(struct socket_ctx *ctx, int socket_id, unsigned ep)
329 const struct acl4_rules *rules_out, *rules_in;
330 unsigned nb_out_rules, nb_in_rules;
333 rte_exit(EXIT_FAILURE, "NULL context.\n");
335 if (ctx->sp_ipv4_in != NULL)
336 rte_exit(EXIT_FAILURE, "Inbound SP DB for socket %u already "
337 "initialized\n", socket_id);
339 if (ctx->sp_ipv4_out != NULL)
340 rte_exit(EXIT_FAILURE, "Outbound SP DB for socket %u already "
341 "initialized\n", socket_id);
344 rules_out = acl4_rules_in;
345 nb_out_rules = RTE_DIM(acl4_rules_in);
346 rules_in = acl4_rules_out;
347 nb_in_rules = RTE_DIM(acl4_rules_out);
348 } else if (ep == 1) {
349 rules_out = acl4_rules_out;
350 nb_out_rules = RTE_DIM(acl4_rules_out);
351 rules_in = acl4_rules_in;
352 nb_in_rules = RTE_DIM(acl4_rules_in);
354 rte_exit(EXIT_FAILURE, "Invalid EP value %u. "
355 "Only 0 or 1 supported.\n", ep);
358 ctx->sp_ipv4_in = (struct sp_ctx *)acl4_init(name, socket_id,
359 rules_in, nb_in_rules);
361 name = "sp_ipv4_out";
362 ctx->sp_ipv4_out = (struct sp_ctx *)acl4_init(name, socket_id,
363 rules_out, nb_out_rules);