4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
39 #include <rte_ethdev.h>
40 #include <rte_ether.h>
42 #include <rte_byteorder.h>
44 #include <rte_port_ring.h>
45 #include <rte_table_hash.h>
47 #include <rte_pipeline.h>
52 translate_options(uint32_t *special, uint32_t *ext, uint32_t *key_size)
54 switch (app.pipeline_type) {
55 case e_APP_PIPELINE_HASH_KEY8_EXT:
56 *special = 0; *ext = 1; *key_size = 8; return;
57 case e_APP_PIPELINE_HASH_KEY8_LRU:
58 *special = 0; *ext = 0; *key_size = 8; return;
59 case e_APP_PIPELINE_HASH_KEY16_EXT:
60 *special = 0; *ext = 1; *key_size = 16; return;
61 case e_APP_PIPELINE_HASH_KEY16_LRU:
62 *special = 0; *ext = 0; *key_size = 16; return;
63 case e_APP_PIPELINE_HASH_KEY32_EXT:
64 *special = 0; *ext = 1; *key_size = 32; return;
65 case e_APP_PIPELINE_HASH_KEY32_LRU:
66 *special = 0; *ext = 0; *key_size = 32; return;
68 case e_APP_PIPELINE_HASH_SPEC_KEY8_EXT:
69 *special = 1; *ext = 1; *key_size = 8; return;
70 case e_APP_PIPELINE_HASH_SPEC_KEY8_LRU:
71 *special = 1; *ext = 0; *key_size = 8; return;
72 case e_APP_PIPELINE_HASH_SPEC_KEY16_EXT:
73 *special = 1; *ext = 1; *key_size = 16; return;
74 case e_APP_PIPELINE_HASH_SPEC_KEY16_LRU:
75 *special = 1; *ext = 0; *key_size = 16; return;
76 case e_APP_PIPELINE_HASH_SPEC_KEY32_EXT:
77 *special = 1; *ext = 1; *key_size = 32; return;
78 case e_APP_PIPELINE_HASH_SPEC_KEY32_LRU:
79 *special = 1; *ext = 0; *key_size = 32; return;
81 case e_APP_PIPELINE_HASH_CUCKOO_KEY8:
82 *special = 0; *ext = 0; *key_size = 8; return;
83 case e_APP_PIPELINE_HASH_CUCKOO_KEY16:
84 *special = 0; *ext = 0; *key_size = 16; return;
85 case e_APP_PIPELINE_HASH_CUCKOO_KEY32:
86 *special = 0; *ext = 0; *key_size = 32; return;
87 case e_APP_PIPELINE_HASH_CUCKOO_KEY48:
88 *special = 0; *ext = 0; *key_size = 48; return;
89 case e_APP_PIPELINE_HASH_CUCKOO_KEY64:
90 *special = 0; *ext = 0; *key_size = 64; return;
91 case e_APP_PIPELINE_HASH_CUCKOO_KEY80:
92 *special = 0; *ext = 0; *key_size = 80; return;
93 case e_APP_PIPELINE_HASH_CUCKOO_KEY96:
94 *special = 0; *ext = 0; *key_size = 96; return;
95 case e_APP_PIPELINE_HASH_CUCKOO_KEY112:
96 *special = 0; *ext = 0; *key_size = 112; return;
97 case e_APP_PIPELINE_HASH_CUCKOO_KEY128:
98 *special = 0; *ext = 0; *key_size = 128; return;
101 rte_panic("Invalid hash table type or key size\n");
105 app_main_loop_worker_pipeline_hash(void) {
106 struct rte_pipeline_params pipeline_params = {
108 .socket_id = rte_socket_id(),
111 struct rte_pipeline *p;
112 uint32_t port_in_id[APP_MAX_PORTS];
113 uint32_t port_out_id[APP_MAX_PORTS];
116 uint32_t special, ext, key_size;
118 translate_options(&special, &ext, &key_size);
120 RTE_LOG(INFO, USER1, "Core %u is doing work "
121 "(pipeline with hash table, %s, %s, %d-byte key)\n",
123 special ? "specialized" : "non-specialized",
124 ext ? "extendible bucket" : "LRU",
127 /* Pipeline configuration */
128 p = rte_pipeline_create(&pipeline_params);
130 rte_panic("Unable to configure the pipeline\n");
132 /* Input port configuration */
133 for (i = 0; i < app.n_ports; i++) {
134 struct rte_port_ring_reader_params port_ring_params = {
135 .ring = app.rings_rx[i],
138 struct rte_pipeline_port_in_params port_params = {
139 .ops = &rte_port_ring_reader_ops,
140 .arg_create = (void *) &port_ring_params,
143 .burst_size = app.burst_size_worker_read,
146 if (rte_pipeline_port_in_create(p, &port_params,
148 rte_panic("Unable to configure input port for "
152 /* Output port configuration */
153 for (i = 0; i < app.n_ports; i++) {
154 struct rte_port_ring_writer_params port_ring_params = {
155 .ring = app.rings_tx[i],
156 .tx_burst_sz = app.burst_size_worker_write,
159 struct rte_pipeline_port_out_params port_params = {
160 .ops = &rte_port_ring_writer_ops,
161 .arg_create = (void *) &port_ring_params,
166 if (rte_pipeline_port_out_create(p, &port_params,
168 rte_panic("Unable to configure output port for "
172 struct rte_table_hash_params table_hash_params = {
174 .key_size = key_size,
175 .key_offset = APP_METADATA_OFFSET(32),
178 .n_buckets = 1 << 22,
183 /* Table configuration */
184 switch (app.pipeline_type) {
185 case e_APP_PIPELINE_HASH_KEY8_EXT:
186 case e_APP_PIPELINE_HASH_KEY16_EXT:
187 case e_APP_PIPELINE_HASH_KEY32_EXT:
189 struct rte_pipeline_table_params table_params = {
190 .ops = &rte_table_hash_ext_ops,
191 .arg_create = &table_hash_params,
192 .f_action_hit = NULL,
193 .f_action_miss = NULL,
195 .action_data_size = 0,
198 if (rte_pipeline_table_create(p, &table_params, &table_id))
199 rte_panic("Unable to configure the hash table\n");
203 case e_APP_PIPELINE_HASH_KEY8_LRU:
204 case e_APP_PIPELINE_HASH_KEY16_LRU:
205 case e_APP_PIPELINE_HASH_KEY32_LRU:
207 struct rte_pipeline_table_params table_params = {
208 .ops = &rte_table_hash_lru_ops,
209 .arg_create = &table_hash_params,
210 .f_action_hit = NULL,
211 .f_action_miss = NULL,
213 .action_data_size = 0,
216 if (rte_pipeline_table_create(p, &table_params, &table_id))
217 rte_panic("Unable to configure the hash table\n");
221 case e_APP_PIPELINE_HASH_SPEC_KEY8_EXT:
223 struct rte_pipeline_table_params table_params = {
224 .ops = &rte_table_hash_key8_ext_ops,
225 .arg_create = &table_hash_params,
226 .f_action_hit = NULL,
227 .f_action_miss = NULL,
229 .action_data_size = 0,
232 if (rte_pipeline_table_create(p, &table_params, &table_id))
233 rte_panic("Unable to configure the hash table\n");
237 case e_APP_PIPELINE_HASH_SPEC_KEY8_LRU:
239 struct rte_pipeline_table_params table_params = {
240 .ops = &rte_table_hash_key8_lru_ops,
241 .arg_create = &table_hash_params,
242 .f_action_hit = NULL,
243 .f_action_miss = NULL,
245 .action_data_size = 0,
248 if (rte_pipeline_table_create(p, &table_params, &table_id))
249 rte_panic("Unable to configure the hash table\n");
253 case e_APP_PIPELINE_HASH_SPEC_KEY16_EXT:
255 struct rte_pipeline_table_params table_params = {
256 .ops = &rte_table_hash_key16_ext_ops,
257 .arg_create = &table_hash_params,
258 .f_action_hit = NULL,
259 .f_action_miss = NULL,
261 .action_data_size = 0,
264 if (rte_pipeline_table_create(p, &table_params, &table_id))
265 rte_panic("Unable to configure the hash table)\n");
269 case e_APP_PIPELINE_HASH_SPEC_KEY16_LRU:
271 struct rte_pipeline_table_params table_params = {
272 .ops = &rte_table_hash_key16_lru_ops,
273 .arg_create = &table_hash_params,
274 .f_action_hit = NULL,
275 .f_action_miss = NULL,
277 .action_data_size = 0,
280 if (rte_pipeline_table_create(p, &table_params, &table_id))
281 rte_panic("Unable to configure the hash table\n");
285 case e_APP_PIPELINE_HASH_SPEC_KEY32_EXT:
287 struct rte_pipeline_table_params table_params = {
288 .ops = &rte_table_hash_key32_ext_ops,
289 .arg_create = &table_hash_params,
290 .f_action_hit = NULL,
291 .f_action_miss = NULL,
293 .action_data_size = 0,
296 if (rte_pipeline_table_create(p, &table_params, &table_id))
297 rte_panic("Unable to configure the hash table\n");
302 case e_APP_PIPELINE_HASH_SPEC_KEY32_LRU:
304 struct rte_pipeline_table_params table_params = {
305 .ops = &rte_table_hash_key32_lru_ops,
306 .arg_create = &table_hash_params,
307 .f_action_hit = NULL,
308 .f_action_miss = NULL,
310 .action_data_size = 0,
313 if (rte_pipeline_table_create(p, &table_params, &table_id))
314 rte_panic("Unable to configure the hash table\n");
318 case e_APP_PIPELINE_HASH_CUCKOO_KEY8:
319 case e_APP_PIPELINE_HASH_CUCKOO_KEY16:
320 case e_APP_PIPELINE_HASH_CUCKOO_KEY32:
321 case e_APP_PIPELINE_HASH_CUCKOO_KEY48:
322 case e_APP_PIPELINE_HASH_CUCKOO_KEY64:
323 case e_APP_PIPELINE_HASH_CUCKOO_KEY80:
324 case e_APP_PIPELINE_HASH_CUCKOO_KEY96:
325 case e_APP_PIPELINE_HASH_CUCKOO_KEY112:
326 case e_APP_PIPELINE_HASH_CUCKOO_KEY128:
328 struct rte_pipeline_table_params table_params = {
329 .ops = &rte_table_hash_cuckoo_ops,
330 .arg_create = &table_hash_params,
331 .f_action_hit = NULL,
332 .f_action_miss = NULL,
334 .action_data_size = 0,
337 if (rte_pipeline_table_create(p, &table_params, &table_id))
338 rte_panic("Unable to configure the hash table\n");
343 rte_panic("Invalid hash table type or key size\n");
346 /* Interconnecting ports and tables */
347 for (i = 0; i < app.n_ports; i++)
348 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
350 rte_panic("Unable to connect input port %u to "
351 "table %u\n", port_in_id[i], table_id);
353 /* Add entries to tables */
354 for (i = 0; i < (1 << 24); i++) {
355 struct rte_pipeline_table_entry entry = {
356 .action = RTE_PIPELINE_ACTION_PORT,
357 {.port_id = port_out_id[i & (app.n_ports - 1)]},
359 struct rte_pipeline_table_entry *entry_ptr;
361 uint32_t *k32 = (uint32_t *) key;
362 int key_found, status;
364 memset(key, 0, sizeof(key));
365 k32[0] = rte_be_to_cpu_32(i);
367 status = rte_pipeline_table_entry_add(p, table_id, key, &entry,
368 &key_found, &entry_ptr);
370 rte_panic("Unable to add entry to table %u (%d)\n",
374 /* Enable input ports */
375 for (i = 0; i < app.n_ports; i++)
376 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
377 rte_panic("Unable to enable input port %u\n",
380 /* Check pipeline consistency */
381 if (rte_pipeline_check(p) < 0)
382 rte_panic("Pipeline consistency check failed\n");
392 if ((i & APP_FLUSH) == 0)
393 rte_pipeline_flush(p);
400 __attribute__((unused)) void *key_mask,
401 __attribute__((unused)) uint32_t key_size,
402 __attribute__((unused)) uint64_t seed)
405 uint32_t ip_dst = rte_be_to_cpu_32(k32[0]);
406 uint64_t signature = (ip_dst >> 2) | ((ip_dst & 0x3) << 30);
412 app_main_loop_rx_metadata(void) {
416 RTE_LOG(INFO, USER1, "Core %u is doing RX (with meta-data)\n",
419 for (i = 0; ; i = ((i + 1) & (app.n_ports - 1))) {
422 n_mbufs = rte_eth_rx_burst(
426 app.burst_size_rx_read);
431 for (j = 0; j < n_mbufs; j++) {
433 uint8_t *m_data, *key;
434 struct ipv4_hdr *ip_hdr;
435 struct ipv6_hdr *ipv6_hdr;
438 uint32_t *signature, *k32;
440 m = app.mbuf_rx.array[j];
441 m_data = rte_pktmbuf_mtod(m, uint8_t *);
442 signature = RTE_MBUF_METADATA_UINT32_PTR(m,
443 APP_METADATA_OFFSET(0));
444 key = RTE_MBUF_METADATA_UINT8_PTR(m,
445 APP_METADATA_OFFSET(32));
447 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
448 ip_hdr = (struct ipv4_hdr *)
449 &m_data[sizeof(struct ether_hdr)];
450 ip_dst = ip_hdr->dst_addr;
452 k32 = (uint32_t *) key;
453 k32[0] = ip_dst & 0xFFFFFF00;
454 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
455 ipv6_hdr = (struct ipv6_hdr *)
456 &m_data[sizeof(struct ether_hdr)];
457 ipv6_dst = ipv6_hdr->dst_addr;
459 memcpy(key, ipv6_dst, 16);
463 *signature = test_hash(key, NULL, 0, 0);
467 ret = rte_ring_sp_enqueue_bulk(
469 (void **) app.mbuf_rx.array,