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