app/test: convert all tests to register system
[dpdk.git] / app / test / test_table.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
35 #ifdef RTE_LIBRTE_TABLE
36
37 #include <rte_byteorder.h>
38 #include <rte_hexdump.h>
39 #include <rte_string_fns.h>
40 #include <string.h>
41 #include "test.h"
42 #include "test_table.h"
43 #include "test_table_pipeline.h"
44 #include "test_table_ports.h"
45 #include "test_table_tables.h"
46 #include "test_table_combined.h"
47 #include "test_table_acl.h"
48
49 /* Global variables */
50 struct rte_pipeline *p;
51 struct rte_ring *rings_rx[N_PORTS];
52 struct rte_ring *rings_tx[N_PORTS];
53 struct rte_mempool *pool = NULL;
54
55 uint32_t port_in_id[N_PORTS];
56 uint32_t port_out_id[N_PORTS];
57 uint32_t port_out_id_type[3];
58 uint32_t table_id[N_PORTS*2];
59 uint64_t override_hit_mask = 0xFFFFFFFF;
60 uint64_t override_miss_mask = 0xFFFFFFFF;
61 uint64_t non_reserved_actions_hit = 0;
62 uint64_t non_reserved_actions_miss = 0;
63 uint8_t connect_miss_action_to_port_out = 0;
64 uint8_t connect_miss_action_to_table = 0;
65 uint32_t table_entry_default_action = RTE_PIPELINE_ACTION_DROP;
66 uint32_t table_entry_hit_action = RTE_PIPELINE_ACTION_PORT;
67 uint32_t table_entry_miss_action = RTE_PIPELINE_ACTION_DROP;
68 rte_pipeline_port_in_action_handler port_in_action = NULL;
69 rte_pipeline_port_out_action_handler port_out_action = NULL;
70 rte_pipeline_table_action_handler_hit action_handler_hit = NULL;
71 rte_pipeline_table_action_handler_miss action_handler_miss = NULL;
72
73 /* Function prototypes */
74 static void app_init_rings(void);
75 static void app_init_mbuf_pools(void);
76
77 uint64_t pipeline_test_hash(void *key,
78                 __attribute__((unused)) uint32_t key_size,
79                 __attribute__((unused)) uint64_t seed)
80 {
81         uint32_t *k32 = (uint32_t *) key;
82         uint32_t ip_dst = rte_be_to_cpu_32(k32[0]);
83         uint64_t signature = ip_dst;
84
85         return signature;
86 }
87
88 static void
89 app_init_mbuf_pools(void)
90 {
91         /* Init the buffer pool */
92         printf("Getting/Creating the mempool ...\n");
93         pool = rte_mempool_lookup("mempool");
94         if (!pool) {
95                 pool = rte_mempool_create(
96                         "mempool",
97                         POOL_SIZE,
98                         POOL_BUFFER_SIZE,
99                         POOL_CACHE_SIZE,
100                         sizeof(struct rte_pktmbuf_pool_private),
101                         rte_pktmbuf_pool_init, NULL,
102                         rte_pktmbuf_init, NULL,
103                         0,
104                         0);
105                 if (pool == NULL)
106                         rte_panic("Cannot create mbuf pool\n");
107         }
108 }
109
110 static void
111 app_init_rings(void)
112 {
113         uint32_t i;
114
115         for (i = 0; i < N_PORTS; i++) {
116                 char name[32];
117
118                 snprintf(name, sizeof(name), "app_ring_rx_%u", i);
119                 rings_rx[i] = rte_ring_lookup(name);
120                 if (rings_rx[i] == NULL) {
121                         rings_rx[i] = rte_ring_create(
122                                 name,
123                                 RING_RX_SIZE,
124                                 0,
125                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
126                 }
127                 if (rings_rx[i] == NULL)
128                         rte_panic("Cannot create RX ring %u\n", i);
129         }
130
131         for (i = 0; i < N_PORTS; i++) {
132                 char name[32];
133
134                 snprintf(name, sizeof(name), "app_ring_tx_%u", i);
135                 rings_tx[i] = rte_ring_lookup(name);
136                 if (rings_tx[i] == NULL) {
137                         rings_tx[i] = rte_ring_create(
138                                 name,
139                                 RING_TX_SIZE,
140                                 0,
141                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
142                 }
143                 if (rings_tx[i] == NULL)
144                         rte_panic("Cannot create TX ring %u\n", i);
145         }
146
147 }
148
149 static int
150 test_table(void)
151 {
152         int status, failures;
153         unsigned i;
154
155         failures = 0;
156
157         app_init_rings();
158         app_init_mbuf_pools();
159
160         printf("\n\n\n\n************Pipeline tests************\n");
161
162         if (test_table_pipeline() < 0)
163                 return -1;
164
165         printf("\n\n\n\n************Port tests************\n");
166         for (i = 0; i < n_port_tests; i++) {
167                 status = port_tests[i]();
168                 if (status < 0) {
169                         printf("\nPort test number %d failed (%d).\n", i,
170                                 status);
171                         failures++;
172                         return -1;
173                 }
174         }
175
176         printf("\n\n\n\n************Table tests************\n");
177         for (i = 0; i < n_table_tests; i++) {
178                 status = table_tests[i]();
179                 if (status < 0) {
180                         printf("\nTable test number %d failed (%d).\n", i,
181                                 status);
182                         failures++;
183                         return -1;
184                 }
185         }
186
187         printf("\n\n\n\n************Table tests************\n");
188         for (i = 0; i < n_table_tests_combined; i++) {
189                 status = table_tests_combined[i]();
190                 if (status < 0) {
191                         printf("\nCombined table test number %d failed with "
192                                 "reason number %d.\n", i, status);
193                         failures++;
194                         return -1;
195                 }
196         }
197
198         if (failures)
199                 return -1;
200
201 #ifdef RTE_LIBRTE_ACL
202         printf("\n\n\n\n************ACL tests************\n");
203         if (test_table_ACL() < 0)
204                 return -1;
205 #endif
206
207         return 0;
208 }
209
210 static struct test_command table_cmd = {
211         .command = "table_autotest",
212         .callback = test_table,
213 };
214 REGISTER_TEST_COMMAND(table_cmd);
215 #endif