app/test: convert all tests to register system
[dpdk.git] / app / test / test_table_acl.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #ifdef RTE_LIBRTE_ACL
35
36 #include <rte_hexdump.h>
37 #include "test_table.h"
38 #include "test_table_acl.h"
39
40 #define IPv4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) |              \
41         (((b) & 0xff) << 16) |                                          \
42         (((c) & 0xff) << 8) |                                           \
43         ((d) & 0xff))
44
45 /*
46  * Rule and trace formats definitions.
47  **/
48
49 struct ipv4_5tuple {
50         uint8_t  proto;
51         uint32_t ip_src;
52         uint32_t ip_dst;
53         uint16_t port_src;
54         uint16_t port_dst;
55 };
56
57 enum {
58         PROTO_FIELD_IPV4,
59         SRC_FIELD_IPV4,
60         DST_FIELD_IPV4,
61         SRCP_FIELD_IPV4,
62         DSTP_FIELD_IPV4,
63         NUM_FIELDS_IPV4
64 };
65
66 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
67         {
68                 .type = RTE_ACL_FIELD_TYPE_BITMASK,
69                 .size = sizeof(uint8_t),
70                 .field_index = PROTO_FIELD_IPV4,
71                 .input_index = PROTO_FIELD_IPV4,
72                 .offset = offsetof(struct ipv4_5tuple, proto),
73         },
74         {
75                 .type = RTE_ACL_FIELD_TYPE_MASK,
76                 .size = sizeof(uint32_t),
77                 .field_index = SRC_FIELD_IPV4,
78                 .input_index = SRC_FIELD_IPV4,
79                 .offset = offsetof(struct ipv4_5tuple, ip_src),
80         },
81         {
82                 .type = RTE_ACL_FIELD_TYPE_MASK,
83                 .size = sizeof(uint32_t),
84                 .field_index = DST_FIELD_IPV4,
85                 .input_index = DST_FIELD_IPV4,
86                 .offset = offsetof(struct ipv4_5tuple, ip_dst),
87         },
88         {
89                 .type = RTE_ACL_FIELD_TYPE_RANGE,
90                 .size = sizeof(uint16_t),
91                 .field_index = SRCP_FIELD_IPV4,
92                 .input_index = SRCP_FIELD_IPV4,
93                 .offset = offsetof(struct ipv4_5tuple, port_src),
94         },
95         {
96                 .type = RTE_ACL_FIELD_TYPE_RANGE,
97                 .size = sizeof(uint16_t),
98                 .field_index = DSTP_FIELD_IPV4,
99                 .input_index = SRCP_FIELD_IPV4,
100                 .offset = offsetof(struct ipv4_5tuple, port_dst),
101         },
102 };
103
104 struct rte_table_acl_rule_add_params table_acl_IPv4_rule;
105
106 typedef int (*parse_5tuple)(char *text,
107         struct rte_table_acl_rule_add_params *rule);
108
109 /*
110 * The order of the fields in the rule string after the initial '@'
111 */
112 enum {
113         CB_FLD_SRC_ADDR,
114         CB_FLD_DST_ADDR,
115         CB_FLD_SRC_PORT_RANGE,
116         CB_FLD_DST_PORT_RANGE,
117         CB_FLD_PROTO,
118         CB_FLD_NUM,
119 };
120
121
122 #define GET_CB_FIELD(in, fd, base, lim, dlm)                            \
123 do {                                                                    \
124         unsigned long val;                                              \
125         char *end;                                                      \
126                                                                         \
127         errno = 0;                                                      \
128         val = strtoul((in), &end, (base));                              \
129         if (errno != 0 || end[0] != (dlm) || val > (lim))               \
130                 return -EINVAL;                                         \
131         (fd) = (typeof(fd)) val;                                        \
132         (in) = end + 1;                                                 \
133 } while (0)
134
135
136
137
138 static int
139 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
140 {
141         uint8_t a, b, c, d, m;
142
143         GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
144         GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
145         GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
146         GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
147         GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
148
149         addr[0] = IPv4(a, b, c, d);
150         mask_len[0] = m;
151
152         return 0;
153 }
154
155 static int
156 parse_port_range(const char *in, uint16_t *port_low, uint16_t *port_high)
157 {
158         uint16_t a, b;
159
160         GET_CB_FIELD(in, a, 0, UINT16_MAX, ':');
161         GET_CB_FIELD(in, b, 0, UINT16_MAX, 0);
162
163         port_low[0] = a;
164         port_high[0] = b;
165
166         return 0;
167 }
168
169 static int
170 parse_cb_ipv4_rule(char *str, struct rte_table_acl_rule_add_params *v)
171 {
172         int i, rc;
173         char *s, *sp, *in[CB_FLD_NUM];
174         static const char *dlm = " \t\n";
175
176         /*
177         ** Skip leading '@'
178         */
179         if (strchr(str, '@') != str)
180                 return -EINVAL;
181
182         s = str + 1;
183
184         /*
185         * Populate the 'in' array with the location of each
186         * field in the string we're parsing
187         */
188         for (i = 0; i != DIM(in); i++) {
189                 in[i] = strtok_r(s, dlm, &sp);
190                 if (in[i] == NULL)
191                         return -EINVAL;
192                 s = NULL;
193         }
194
195         /* Parse x.x.x.x/x */
196         rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
197                 &v->field_value[SRC_FIELD_IPV4].value.u32,
198                 &v->field_value[SRC_FIELD_IPV4].mask_range.u32);
199         if (rc != 0) {
200                 RTE_LOG(ERR, PIPELINE, "failed to read src address/mask: %s\n",
201                         in[CB_FLD_SRC_ADDR]);
202                 return rc;
203         }
204
205         printf("V=%u, mask=%u\n", v->field_value[SRC_FIELD_IPV4].value.u32,
206                 v->field_value[SRC_FIELD_IPV4].mask_range.u32);
207
208         /* Parse x.x.x.x/x */
209         rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
210                 &v->field_value[DST_FIELD_IPV4].value.u32,
211                 &v->field_value[DST_FIELD_IPV4].mask_range.u32);
212         if (rc != 0) {
213                 RTE_LOG(ERR, PIPELINE, "failed to read dest address/mask: %s\n",
214                         in[CB_FLD_DST_ADDR]);
215                 return rc;
216         }
217
218         printf("V=%u, mask=%u\n", v->field_value[DST_FIELD_IPV4].value.u32,
219         v->field_value[DST_FIELD_IPV4].mask_range.u32);
220         /* Parse n:n */
221         rc = parse_port_range(in[CB_FLD_SRC_PORT_RANGE],
222                 &v->field_value[SRCP_FIELD_IPV4].value.u16,
223                 &v->field_value[SRCP_FIELD_IPV4].mask_range.u16);
224         if (rc != 0) {
225                 RTE_LOG(ERR, PIPELINE, "failed to read source port range: %s\n",
226                         in[CB_FLD_SRC_PORT_RANGE]);
227                 return rc;
228         }
229
230         printf("V=%u, mask=%u\n", v->field_value[SRCP_FIELD_IPV4].value.u16,
231                 v->field_value[SRCP_FIELD_IPV4].mask_range.u16);
232         /* Parse n:n */
233         rc = parse_port_range(in[CB_FLD_DST_PORT_RANGE],
234                 &v->field_value[DSTP_FIELD_IPV4].value.u16,
235                 &v->field_value[DSTP_FIELD_IPV4].mask_range.u16);
236         if (rc != 0) {
237                 RTE_LOG(ERR, PIPELINE, "failed to read dest port range: %s\n",
238                         in[CB_FLD_DST_PORT_RANGE]);
239                 return rc;
240         }
241
242         printf("V=%u, mask=%u\n", v->field_value[DSTP_FIELD_IPV4].value.u16,
243                 v->field_value[DSTP_FIELD_IPV4].mask_range.u16);
244         /* parse 0/0xnn */
245         GET_CB_FIELD(in[CB_FLD_PROTO],
246                 v->field_value[PROTO_FIELD_IPV4].value.u8,
247                 0, UINT8_MAX, '/');
248         GET_CB_FIELD(in[CB_FLD_PROTO],
249                 v->field_value[PROTO_FIELD_IPV4].mask_range.u8,
250                 0, UINT8_MAX, 0);
251
252         printf("V=%u, mask=%u\n",
253                 (unsigned int)v->field_value[PROTO_FIELD_IPV4].value.u8,
254                 v->field_value[PROTO_FIELD_IPV4].mask_range.u8);
255         return 0;
256 }
257
258
259 /*
260  * The format for these rules DO NOT need the port ranges to be
261  * separated by ' : ', just ':'. It's a lot more readable and
262  * cleaner, IMO.
263  */
264 char lines[][128] = {
265         "@0.0.0.0/0 0.0.0.0/0 0:65535 0:65535 2/0xff", /* Protocol check */
266         "@192.168.3.1/32 0.0.0.0/0 0:65535 0:65535 0/0", /* Src IP checl */
267         "@0.0.0.0/0 10.4.4.1/32 0:65535 0:65535 0/0", /* dst IP check */
268         "@0.0.0.0/0 0.0.0.0/0 105:105 0:65535 0/0", /* src port check */
269         "@0.0.0.0/0 0.0.0.0/0 0:65535 206:206 0/0", /* dst port check */
270 };
271
272 char line[128];
273
274
275 static int
276 setup_acl_pipeline(void)
277 {
278         int ret;
279         int i;
280         struct rte_pipeline_params pipeline_params = {
281                 .name = "PIPELINE",
282                 .socket_id = 0,
283         };
284         uint32_t n;
285         struct rte_table_acl_rule_add_params rule_params;
286         struct rte_pipeline_table_acl_rule_delete_params *delete_params;
287         parse_5tuple parser;
288         char acl_name[64];
289
290         /* Pipeline configuration */
291         p = rte_pipeline_create(&pipeline_params);
292         if (p == NULL) {
293                 RTE_LOG(INFO, PIPELINE, "%s: Failed to configure pipeline\n",
294                         __func__);
295                 goto fail;
296         }
297
298         /* Input port configuration */
299         for (i = 0; i < N_PORTS; i++) {
300                 struct rte_port_ring_reader_params port_ring_params = {
301                         .ring = rings_rx[i],
302                 };
303
304                 struct rte_pipeline_port_in_params port_params = {
305                         .ops = &rte_port_ring_reader_ops,
306                         .arg_create = (void *) &port_ring_params,
307                         .f_action = NULL,
308                         .burst_size = BURST_SIZE,
309                 };
310
311                 /* Put in action for some ports */
312                 if (i)
313                         port_params.f_action = port_in_action;
314
315                 ret = rte_pipeline_port_in_create(p, &port_params,
316                         &port_in_id[i]);
317                 if (ret) {
318                         rte_panic("Unable to configure input port %d, ret:%d\n",
319                                 i, ret);
320                         goto fail;
321                 }
322         }
323
324         /* output Port configuration */
325         for (i = 0; i < N_PORTS; i++) {
326                 struct rte_port_ring_writer_params port_ring_params = {
327                         .ring = rings_tx[i],
328                         .tx_burst_sz = BURST_SIZE,
329                 };
330
331                 struct rte_pipeline_port_out_params port_params = {
332                         .ops = &rte_port_ring_writer_ops,
333                         .arg_create = (void *) &port_ring_params,
334                         .f_action = NULL,
335                         .arg_ah = NULL,
336                 };
337
338
339                 if (rte_pipeline_port_out_create(p, &port_params,
340                         &port_out_id[i])) {
341                         rte_panic("Unable to configure output port %d\n", i);
342                         goto fail;
343                 }
344         }
345
346         /* Table configuration  */
347         for (i = 0; i < N_PORTS; i++) {
348                 struct rte_pipeline_table_params table_params;
349
350                 /* Set up defaults for stub */
351                 table_params.ops = &rte_table_stub_ops;
352                 table_params.arg_create = NULL;
353                 table_params.f_action_hit = action_handler_hit;
354                 table_params.f_action_miss = NULL;
355                 table_params.action_data_size = 0;
356
357                 RTE_LOG(INFO, PIPELINE, "miss_action=%x\n",
358                         table_entry_miss_action);
359
360                 printf("RTE_ACL_RULE_SZ(%zu) = %zu\n", DIM(ipv4_defs),
361                         RTE_ACL_RULE_SZ(DIM(ipv4_defs)));
362
363                 struct rte_table_acl_params acl_params;
364
365                 acl_params.n_rules = 1 << 5;
366                 acl_params.n_rule_fields = DIM(ipv4_defs);
367                 snprintf(acl_name, sizeof(acl_name), "ACL%d", i);
368                 acl_params.name = acl_name;
369                 memcpy(acl_params.field_format, ipv4_defs, sizeof(ipv4_defs));
370
371                 table_params.ops = &rte_table_acl_ops;
372                 table_params.arg_create = &acl_params;
373
374                 if (rte_pipeline_table_create(p, &table_params, &table_id[i])) {
375                         rte_panic("Unable to configure table %u\n", i);
376                         goto fail;
377                 }
378
379                 if (connect_miss_action_to_table) {
380                         if (rte_pipeline_table_create(p, &table_params,
381                                 &table_id[i+2])) {
382                                 rte_panic("Unable to configure table %u\n", i);
383                                 goto fail;
384                         }
385                 }
386         }
387
388         for (i = 0; i < N_PORTS; i++) {
389                 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
390                         table_id[i])) {
391                         rte_panic("Unable to connect input port %u to "
392                                 "table %u\n",
393                                 port_in_id[i],  table_id[i]);
394                         goto fail;
395                 }
396         }
397
398         /* Add entries to tables */
399         for (i = 0; i < N_PORTS; i++) {
400                 struct rte_pipeline_table_entry table_entry = {
401                         .action = RTE_PIPELINE_ACTION_PORT,
402                         {.port_id = port_out_id[i^1]},
403                 };
404                 int key_found;
405                 struct rte_pipeline_table_entry *entry_ptr;
406
407                 memset(&rule_params, 0, sizeof(rule_params));
408                 parser = parse_cb_ipv4_rule;
409
410                 for (n = 1; n <= 5; n++) {
411                         snprintf(line, sizeof(line), "%s", lines[n-1]);
412                         printf("PARSING [%s]\n", line);
413
414                         ret = parser(line, &rule_params);
415                         if (ret != 0) {
416                                 RTE_LOG(ERR, PIPELINE,
417                                         "line %u: parse_cb_ipv4vlan_rule"
418                                         " failed, error code: %d (%s)\n",
419                                         n, ret, strerror(-ret));
420                                 return ret;
421                         }
422
423                         rule_params.priority = RTE_ACL_MAX_PRIORITY - n;
424
425                         ret = rte_pipeline_table_entry_add(p, table_id[i],
426                                 &rule_params,
427                                 &table_entry, &key_found, &entry_ptr);
428                         if (ret < 0) {
429                                 rte_panic("Add entry to table %u failed (%d)\n",
430                                         table_id[i], ret);
431                                 goto fail;
432                         }
433                 }
434
435                 /* delete a few rules */
436                 for (n = 2; n <= 3; n++) {
437                         snprintf(line, sizeof(line), "%s", lines[n-1]);
438                         printf("PARSING [%s]\n", line);
439
440                         ret = parser(line, &rule_params);
441                         if (ret != 0) {
442                                 RTE_LOG(ERR, PIPELINE, "line %u: parse rule "
443                                         " failed, error code: %d (%s)\n",
444                                         n, ret, strerror(-ret));
445                                 return ret;
446                         }
447
448                         delete_params = (struct
449                                 rte_pipeline_table_acl_rule_delete_params *)
450                                 &(rule_params.field_value[0]);
451                         ret = rte_pipeline_table_entry_delete(p, table_id[i],
452                                 delete_params, &key_found, NULL);
453                         if (ret < 0) {
454                                 rte_panic("Add entry to table %u failed (%d)\n",
455                                         table_id[i], ret);
456                                 goto fail;
457                         } else
458                                 printf("Deleted Rule.\n");
459                 }
460
461
462                 /* Try to add duplicates */
463                 for (n = 1; n <= 5; n++) {
464                         snprintf(line, sizeof(line), "%s", lines[n-1]);
465                         printf("PARSING [%s]\n", line);
466
467                         ret = parser(line, &rule_params);
468                         if (ret != 0) {
469                                 RTE_LOG(ERR, PIPELINE, "line %u: parse rule"
470                                         " failed, error code: %d (%s)\n",
471                                         n, ret, strerror(-ret));
472                                 return ret;
473                         }
474
475                         rule_params.priority = RTE_ACL_MAX_PRIORITY - n;
476
477                         ret = rte_pipeline_table_entry_add(p, table_id[i],
478                                 &rule_params,
479                                 &table_entry, &key_found, &entry_ptr);
480                         if (ret < 0) {
481                                 rte_panic("Add entry to table %u failed (%d)\n",
482                                         table_id[i], ret);
483                                 goto fail;
484                         }
485                 }
486         }
487
488         /* Enable input ports */
489         for (i = 0; i < N_PORTS ; i++)
490                 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
491                         rte_panic("Unable to enable input port %u\n",
492                                 port_in_id[i]);
493
494         /* Check pipeline consistency */
495         if (rte_pipeline_check(p) < 0) {
496                 rte_panic("Pipeline consistency check failed\n");
497                 goto fail;
498         }
499
500         return  0;
501 fail:
502
503         return -1;
504 }
505
506 static int
507 test_pipeline_single_filter(int expected_count)
508 {
509         int i, j, ret, tx_count;
510         struct ipv4_5tuple five_tuple;
511
512         /* Allocate a few mbufs and manually insert into the rings. */
513         for (i = 0; i < N_PORTS; i++) {
514                 for (j = 0; j < 8; j++) {
515                         struct rte_mbuf *mbuf;
516
517                         mbuf = rte_pktmbuf_alloc(pool);
518                         memset(mbuf->pkt.data, 0x00,
519                                 sizeof(struct ipv4_5tuple));
520
521                         five_tuple.proto = j;
522                         five_tuple.ip_src = rte_bswap32(IPv4(192, 168, j, 1));
523                         five_tuple.ip_dst = rte_bswap32(IPv4(10, 4, j, 1));
524                         five_tuple.port_src = rte_bswap16(100 + j);
525                         five_tuple.port_dst = rte_bswap16(200 + j);
526
527                         memcpy(mbuf->pkt.data, &five_tuple,
528                                 sizeof(struct ipv4_5tuple));
529                         RTE_LOG(INFO, PIPELINE, "%s: Enqueue onto ring %d\n",
530                                 __func__, i);
531                         rte_ring_enqueue(rings_rx[i], mbuf);
532                 }
533         }
534
535         /* Run pipeline once */
536         rte_pipeline_run(p);
537
538         rte_pipeline_flush(p);
539
540         tx_count = 0;
541
542         for (i = 0; i < N_PORTS; i++) {
543                 void *objs[RING_TX_SIZE];
544                 struct rte_mbuf *mbuf;
545
546                 ret = rte_ring_sc_dequeue_burst(rings_tx[i], objs, 10);
547                 if (ret <= 0) {
548                         printf("Got no objects from ring %d - error code %d\n",
549                                 i, ret);
550                 } else {
551                         printf("Got %d object(s) from ring %d!\n", ret, i);
552                         for (j = 0; j < ret; j++) {
553                                 mbuf = (struct rte_mbuf *)objs[j];
554                                 rte_hexdump(stdout, "mbuf", mbuf->pkt.data, 64);
555                                 rte_pktmbuf_free(mbuf);
556                         }
557                         tx_count += ret;
558                 }
559         }
560
561         if (tx_count != expected_count) {
562                 RTE_LOG(INFO, PIPELINE,
563                         "%s: Unexpected packets for ACL test, "
564                         "expected %d, got %d\n",
565                         __func__, expected_count, tx_count);
566                 goto fail;
567         }
568
569         rte_pipeline_free(p);
570
571         return  0;
572 fail:
573         return -1;
574
575 }
576
577 int
578 test_table_ACL(void)
579 {
580
581
582         override_hit_mask = 0xFF; /* All packets are a hit */
583
584         setup_acl_pipeline();
585         if (test_pipeline_single_filter(10) < 0)
586                 return -1;
587
588         return 0;
589 }
590
591 #endif