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 /* Table configuration */
173 switch (app.pipeline_type) {
174 case e_APP_PIPELINE_HASH_KEY8_EXT:
175 case e_APP_PIPELINE_HASH_KEY16_EXT:
176 case e_APP_PIPELINE_HASH_KEY32_EXT:
178 struct rte_table_hash_params table_hash_params = {
180 .key_size = key_size,
181 .key_offset = APP_METADATA_OFFSET(32),
184 .n_buckets = 1 << 22,
185 .f_hash = (rte_table_hash_op_hash)test_hash,
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_table_hash_params table_hash_params = {
209 .key_size = key_size,
210 .key_offset = APP_METADATA_OFFSET(32),
213 .n_buckets = 1 << 22,
214 .f_hash = (rte_table_hash_op_hash)test_hash,
218 struct rte_pipeline_table_params table_params = {
219 .ops = &rte_table_hash_lru_ops,
220 .arg_create = &table_hash_params,
221 .f_action_hit = NULL,
222 .f_action_miss = NULL,
224 .action_data_size = 0,
227 if (rte_pipeline_table_create(p, &table_params, &table_id))
228 rte_panic("Unable to configure the hash table\n");
232 case e_APP_PIPELINE_HASH_SPEC_KEY8_EXT:
234 struct rte_table_hash_params table_hash_params = {
236 .key_size = key_size,
237 .key_offset = APP_METADATA_OFFSET(32),
240 .n_buckets = 1 << 22,
241 .f_hash = (rte_table_hash_op_hash)test_hash,
245 struct rte_pipeline_table_params table_params = {
246 .ops = &rte_table_hash_key8_ext_ops,
247 .arg_create = &table_hash_params,
248 .f_action_hit = NULL,
249 .f_action_miss = NULL,
251 .action_data_size = 0,
254 if (rte_pipeline_table_create(p, &table_params, &table_id))
255 rte_panic("Unable to configure the hash table\n");
259 case e_APP_PIPELINE_HASH_SPEC_KEY8_LRU:
261 struct rte_table_hash_params table_hash_params = {
263 .key_size = key_size,
264 .key_offset = APP_METADATA_OFFSET(32),
267 .n_buckets = 1 << 22,
268 .f_hash = (rte_table_hash_op_hash)test_hash,
272 struct rte_pipeline_table_params table_params = {
273 .ops = &rte_table_hash_key8_lru_ops,
274 .arg_create = &table_hash_params,
275 .f_action_hit = NULL,
276 .f_action_miss = NULL,
278 .action_data_size = 0,
281 if (rte_pipeline_table_create(p, &table_params, &table_id))
282 rte_panic("Unable to configure the hash table\n");
286 case e_APP_PIPELINE_HASH_SPEC_KEY16_EXT:
288 struct rte_table_hash_params table_hash_params = {
290 .key_size = key_size,
291 .key_offset = APP_METADATA_OFFSET(32),
294 .n_buckets = 1 << 22,
295 .f_hash = (rte_table_hash_op_hash)test_hash,
299 struct rte_pipeline_table_params table_params = {
300 .ops = &rte_table_hash_key16_ext_ops,
301 .arg_create = &table_hash_params,
302 .f_action_hit = NULL,
303 .f_action_miss = NULL,
305 .action_data_size = 0,
308 if (rte_pipeline_table_create(p, &table_params, &table_id))
309 rte_panic("Unable to configure the hash table)\n");
313 case e_APP_PIPELINE_HASH_SPEC_KEY16_LRU:
315 struct rte_table_hash_params table_hash_params = {
317 .key_size = key_size,
318 .key_offset = APP_METADATA_OFFSET(32),
321 .n_buckets = 1 << 22,
322 .f_hash = (rte_table_hash_op_hash)test_hash,
326 struct rte_pipeline_table_params table_params = {
327 .ops = &rte_table_hash_key16_lru_ops,
328 .arg_create = &table_hash_params,
329 .f_action_hit = NULL,
330 .f_action_miss = NULL,
332 .action_data_size = 0,
335 if (rte_pipeline_table_create(p, &table_params, &table_id))
336 rte_panic("Unable to configure the hash table\n");
340 case e_APP_PIPELINE_HASH_SPEC_KEY32_EXT:
342 struct rte_table_hash_params table_hash_params = {
344 .key_size = key_size,
345 .key_offset = APP_METADATA_OFFSET(32),
348 .n_buckets = 1 << 22,
349 .f_hash = (rte_table_hash_op_hash)test_hash,
353 struct rte_pipeline_table_params table_params = {
354 .ops = &rte_table_hash_key32_ext_ops,
355 .arg_create = &table_hash_params,
356 .f_action_hit = NULL,
357 .f_action_miss = NULL,
359 .action_data_size = 0,
362 if (rte_pipeline_table_create(p, &table_params, &table_id))
363 rte_panic("Unable to configure the hash table\n");
368 case e_APP_PIPELINE_HASH_SPEC_KEY32_LRU:
370 struct rte_table_hash_params table_hash_params = {
372 .key_size = key_size,
373 .key_offset = APP_METADATA_OFFSET(32),
376 .n_buckets = 1 << 22,
377 .f_hash = (rte_table_hash_op_hash)test_hash,
381 struct rte_pipeline_table_params table_params = {
382 .ops = &rte_table_hash_key32_lru_ops,
383 .arg_create = &table_hash_params,
384 .f_action_hit = NULL,
385 .f_action_miss = NULL,
387 .action_data_size = 0,
390 if (rte_pipeline_table_create(p, &table_params, &table_id))
391 rte_panic("Unable to configure the hash table\n");
395 case e_APP_PIPELINE_HASH_CUCKOO_KEY8:
396 case e_APP_PIPELINE_HASH_CUCKOO_KEY16:
397 case e_APP_PIPELINE_HASH_CUCKOO_KEY32:
398 case e_APP_PIPELINE_HASH_CUCKOO_KEY48:
399 case e_APP_PIPELINE_HASH_CUCKOO_KEY64:
400 case e_APP_PIPELINE_HASH_CUCKOO_KEY80:
401 case e_APP_PIPELINE_HASH_CUCKOO_KEY96:
402 case e_APP_PIPELINE_HASH_CUCKOO_KEY112:
403 case e_APP_PIPELINE_HASH_CUCKOO_KEY128:
405 struct rte_table_hash_params table_hash_params = {
407 .key_size = key_size,
408 .key_offset = APP_METADATA_OFFSET(32),
411 .n_buckets = 1 << 22,
412 .f_hash = (rte_table_hash_op_hash)test_hash,
416 struct rte_pipeline_table_params table_params = {
417 .ops = &rte_table_hash_cuckoo_ops,
418 .arg_create = &table_hash_params,
419 .f_action_hit = NULL,
420 .f_action_miss = NULL,
422 .action_data_size = 0,
425 if (rte_pipeline_table_create(p, &table_params, &table_id))
426 rte_panic("Unable to configure the hash table\n");
431 rte_panic("Invalid hash table type or key size\n");
434 /* Interconnecting ports and tables */
435 for (i = 0; i < app.n_ports; i++)
436 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
438 rte_panic("Unable to connect input port %u to "
439 "table %u\n", port_in_id[i], table_id);
441 /* Add entries to tables */
442 for (i = 0; i < (1 << 24); i++) {
443 struct rte_pipeline_table_entry entry = {
444 .action = RTE_PIPELINE_ACTION_PORT,
445 {.port_id = port_out_id[i & (app.n_ports - 1)]},
447 struct rte_pipeline_table_entry *entry_ptr;
449 uint32_t *k32 = (uint32_t *) key;
450 int key_found, status;
452 memset(key, 0, sizeof(key));
453 k32[0] = rte_be_to_cpu_32(i);
455 status = rte_pipeline_table_entry_add(p, table_id, key, &entry,
456 &key_found, &entry_ptr);
458 rte_panic("Unable to add entry to table %u (%d)\n",
462 /* Enable input ports */
463 for (i = 0; i < app.n_ports; i++)
464 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
465 rte_panic("Unable to enable input port %u\n",
468 /* Check pipeline consistency */
469 if (rte_pipeline_check(p) < 0)
470 rte_panic("Pipeline consistency check failed\n");
480 if ((i & APP_FLUSH) == 0)
481 rte_pipeline_flush(p);
488 __attribute__((unused)) uint32_t key_size,
489 __attribute__((unused)) uint64_t seed)
492 uint32_t ip_dst = rte_be_to_cpu_32(k32[0]);
493 uint64_t signature = (ip_dst >> 2) | ((ip_dst & 0x3) << 30);
499 app_main_loop_rx_metadata(void) {
503 RTE_LOG(INFO, USER1, "Core %u is doing RX (with meta-data)\n",
506 for (i = 0; ; i = ((i + 1) & (app.n_ports - 1))) {
509 n_mbufs = rte_eth_rx_burst(
513 app.burst_size_rx_read);
518 for (j = 0; j < n_mbufs; j++) {
520 uint8_t *m_data, *key;
521 struct ipv4_hdr *ip_hdr;
522 struct ipv6_hdr *ipv6_hdr;
525 uint32_t *signature, *k32;
527 m = app.mbuf_rx.array[j];
528 m_data = rte_pktmbuf_mtod(m, uint8_t *);
529 signature = RTE_MBUF_METADATA_UINT32_PTR(m,
530 APP_METADATA_OFFSET(0));
531 key = RTE_MBUF_METADATA_UINT8_PTR(m,
532 APP_METADATA_OFFSET(32));
534 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
535 ip_hdr = (struct ipv4_hdr *)
536 &m_data[sizeof(struct ether_hdr)];
537 ip_dst = ip_hdr->dst_addr;
539 k32 = (uint32_t *) key;
540 k32[0] = ip_dst & 0xFFFFFF00;
541 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
542 ipv6_hdr = (struct ipv6_hdr *)
543 &m_data[sizeof(struct ether_hdr)];
544 ipv6_dst = ipv6_hdr->dst_addr;
546 memcpy(key, ipv6_dst, 16);
550 *signature = test_hash(key, 0, 0);
554 ret = rte_ring_sp_enqueue_bulk(
556 (void **) app.mbuf_rx.array,