New upstream version 17.11-rc3
[deb_dpdk.git] / test / 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)) void *key_mask,
76                 __attribute__((unused)) uint32_t key_size,
77                 __attribute__((unused)) uint64_t seed)
78 {
79         uint32_t *k32 = key;
80         uint32_t ip_dst = rte_be_to_cpu_32(k32[0]);
81         uint64_t signature = ip_dst;
82
83         return signature;
84 }
85
86 static void
87 app_init_mbuf_pools(void)
88 {
89         /* Init the buffer pool */
90         printf("Getting/Creating the mempool ...\n");
91         pool = rte_mempool_lookup("mempool");
92         if (!pool) {
93                 pool = rte_pktmbuf_pool_create(
94                         "mempool",
95                         POOL_SIZE,
96                         POOL_CACHE_SIZE, 0, POOL_BUFFER_SIZE,
97                         0);
98                 if (pool == NULL)
99                         rte_panic("Cannot create mbuf pool\n");
100         }
101 }
102
103 static void
104 app_init_rings(void)
105 {
106         uint32_t i;
107
108         for (i = 0; i < N_PORTS; i++) {
109                 char name[32];
110
111                 snprintf(name, sizeof(name), "app_ring_rx_%u", i);
112                 rings_rx[i] = rte_ring_lookup(name);
113                 if (rings_rx[i] == NULL) {
114                         rings_rx[i] = rte_ring_create(
115                                 name,
116                                 RING_RX_SIZE,
117                                 0,
118                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
119                 }
120                 if (rings_rx[i] == NULL)
121                         rte_panic("Cannot create RX ring %u\n", i);
122         }
123
124         for (i = 0; i < N_PORTS; i++) {
125                 char name[32];
126
127                 snprintf(name, sizeof(name), "app_ring_tx_%u", i);
128                 rings_tx[i] = rte_ring_lookup(name);
129                 if (rings_tx[i] == NULL) {
130                         rings_tx[i] = rte_ring_create(
131                                 name,
132                                 RING_TX_SIZE,
133                                 0,
134                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
135                 }
136                 if (rings_tx[i] == NULL)
137                         rte_panic("Cannot create TX ring %u\n", i);
138         }
139
140 }
141
142 static int
143 test_table(void)
144 {
145         int status, failures;
146         unsigned i;
147
148         failures = 0;
149
150         app_init_rings();
151         app_init_mbuf_pools();
152
153         printf("\n\n\n\n************Pipeline tests************\n");
154
155         if (test_table_pipeline() < 0)
156                 return -1;
157
158         printf("\n\n\n\n************Port tests************\n");
159         for (i = 0; i < n_port_tests; i++) {
160                 status = port_tests[i]();
161                 if (status < 0) {
162                         printf("\nPort test number %d failed (%d).\n", i,
163                                 status);
164                         failures++;
165                         return -1;
166                 }
167         }
168
169         printf("\n\n\n\n************Table tests************\n");
170         for (i = 0; i < n_table_tests; i++) {
171                 status = table_tests[i]();
172                 if (status < 0) {
173                         printf("\nTable test number %d failed (%d).\n", i,
174                                 status);
175                         failures++;
176                         return -1;
177                 }
178         }
179
180         printf("\n\n\n\n************Table tests************\n");
181         for (i = 0; i < n_table_tests_combined; i++) {
182                 status = table_tests_combined[i]();
183                 if (status < 0) {
184                         printf("\nCombined table test number %d failed with "
185                                 "reason number %d.\n", i, status);
186                         failures++;
187                         return -1;
188                 }
189         }
190
191         if (failures)
192                 return -1;
193
194 #ifdef RTE_LIBRTE_ACL
195         printf("\n\n\n\n************ACL tests************\n");
196         if (test_table_acl() < 0)
197                 return -1;
198 #endif
199
200         return 0;
201 }
202
203 REGISTER_TEST_COMMAND(table_autotest, test_table);