examples/ipsec-secgw: add IPsec sample application
[dpdk.git] / examples / ipsec-secgw / sp.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 /*
35  * Security Policies
36  */
37 #include <netinet/ip.h>
38
39 #include <rte_acl.h>
40
41 #include "ipsec.h"
42
43 #define MAX_ACL_RULE_NUM        1000
44
45 /*
46  * Rule and trace formats definitions.
47  */
48 enum {
49         PROTO_FIELD_IPV4,
50         SRC_FIELD_IPV4,
51         DST_FIELD_IPV4,
52         SRCP_FIELD_IPV4,
53         DSTP_FIELD_IPV4,
54         NUM_FIELDS_IPV4
55 };
56
57 /*
58  * That effectively defines order of IPV4 classifications:
59  *  - PROTO
60  *  - SRC IP ADDRESS
61  *  - DST IP ADDRESS
62  *  - PORTS (SRC and DST)
63  */
64 enum {
65         RTE_ACL_IPV4_PROTO,
66         RTE_ACL_IPV4_SRC,
67         RTE_ACL_IPV4_DST,
68         RTE_ACL_IPV4_PORTS,
69         RTE_ACL_IPV4_NUM
70 };
71
72 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
73         {
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,
78         .offset = 0,
79         },
80         {
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)
86         },
87         {
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)
93         },
94         {
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)
100         },
101         {
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) +
107                 sizeof(uint16_t)
108         },
109 };
110
111 RTE_ACL_RULE_DEF(acl4_rules, RTE_DIM(ipv4_defs));
112
113 const struct acl4_rules acl4_rules_in[] = {
114         {
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,},
119         /* source port */
120         .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
121         /* destination port */
122         .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
123         },
124         {
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,},
129         /* source port */
130         .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
131         /* destination port */
132         .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
133         },
134         {
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,},
139         /* source port */
140         .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
141         /* destination port */
142         .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
143         },
144         {
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,},
149         /* source port */
150         .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
151         /* destination port */
152         .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
153         },
154         {
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,},
159         /* source port */
160         .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
161         /* destination port */
162         .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
163         },
164         {
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,},
169         /* source port */
170         .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
171         /* destination port */
172         .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
173         }
174 };
175
176 const struct acl4_rules acl4_rules_out[] = {
177         {
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,},
182         /* source port */
183         .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
184         /* destination port */
185         .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
186         },
187         {
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,},
192         /* source port */
193         .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
194         /* destination port */
195         .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
196         },
197         {
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,},
202         /* source port */
203         .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
204         /* destination port */
205         .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
206         },
207         {
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,},
212         /* source port */
213         .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
214         /* destination port */
215         .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
216         },
217         {
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,},
222         /* source port */
223         .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
224         /* destination port */
225         .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
226         },
227         {
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,},
232         /* source port */
233         .field[3] = {.value.u16 = 0, .mask_range.u16 = 0xffff,},
234         /* destination port */
235         .field[4] = {.value.u16 = 0, .mask_range.u16 = 0xffff,}
236         }
237 };
238
239 static void
240 print_one_ipv4_rule(const struct acl4_rules *rule, int extra)
241 {
242         unsigned char a, b, c, d;
243
244         uint32_t_to_char(rule->field[SRC_FIELD_IPV4].value.u32,
245                         &a, &b, &c, &d);
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,
249                         &a, &b, &c, &d);
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);
259         if (extra)
260                 printf("0x%x-0x%x-0x%x ",
261                         rule->data.category_mask,
262                         rule->data.priority,
263                         rule->data.userdata);
264 }
265
266 static inline void
267 dump_ipv4_rules(const struct acl4_rules *rule, int num, int extra)
268 {
269         int i;
270
271         for (i = 0; i < num; i++, rule++) {
272                 printf("\t%d:", i + 1);
273                 print_one_ipv4_rule(rule, extra);
274                 printf("\n");
275         }
276 }
277
278 static struct rte_acl_ctx *
279 acl4_init(const char *name, int socketid, const struct acl4_rules *rules,
280                 unsigned rules_nb)
281 {
282         char s[PATH_MAX];
283         struct rte_acl_param acl_param;
284         struct rte_acl_config acl_build_param;
285         struct rte_acl_ctx *ctx;
286
287         printf("Creating SP context with %u max rules\n", MAX_ACL_RULE_NUM);
288
289         memset(&acl_param, 0, sizeof(acl_param));
290
291         /* Create ACL contexts */
292         snprintf(s, sizeof(s), "%s_%d", name, socketid);
293
294         printf("IPv4 %s entries [%u]:\n", s, rules_nb);
295         dump_ipv4_rules(rules, rules_nb, 1);
296
297         acl_param.name = s;
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;
301
302         ctx = rte_acl_create(&acl_param);
303         if (ctx == NULL)
304                 rte_exit(EXIT_FAILURE, "Failed to create ACL context\n");
305
306         if (rte_acl_add_rules(ctx, (const struct rte_acl_rule *)rules,
307                                 rules_nb) < 0)
308                 rte_exit(EXIT_FAILURE, "add rules failed\n");
309
310         /* Perform builds */
311         memset(&acl_build_param, 0, sizeof(acl_build_param));
312
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));
316
317         if (rte_acl_build(ctx, &acl_build_param) != 0)
318                 rte_exit(EXIT_FAILURE, "Failed to build ACL trie\n");
319
320         rte_acl_dump(ctx);
321
322         return ctx;
323 }
324
325 void
326 sp_init(struct socket_ctx *ctx, int socket_id, unsigned ep)
327 {
328         const char *name;
329         const struct acl4_rules *rules_out, *rules_in;
330         unsigned nb_out_rules, nb_in_rules;
331
332         if (ctx == NULL)
333                 rte_exit(EXIT_FAILURE, "NULL context.\n");
334
335         if (ctx->sp_ipv4_in != NULL)
336                 rte_exit(EXIT_FAILURE, "Inbound SP DB for socket %u already "
337                                 "initialized\n", socket_id);
338
339         if (ctx->sp_ipv4_out != NULL)
340                 rte_exit(EXIT_FAILURE, "Outbound SP DB for socket %u already "
341                                 "initialized\n", socket_id);
342
343         if (ep == 0) {
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);
353         } else
354                 rte_exit(EXIT_FAILURE, "Invalid EP value %u. "
355                                 "Only 0 or 1 supported.\n", ep);
356
357         name = "sp_ipv4_in";
358         ctx->sp_ipv4_in = (struct sp_ctx *)acl4_init(name, socket_id,
359                         rules_in, nb_in_rules);
360
361         name = "sp_ipv4_out";
362         ctx->sp_ipv4_out = (struct sp_ctx *)acl4_init(name, socket_id,
363                         rules_out, nb_out_rules);
364 }