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_key8_ext_params table_hash_params = {
235 .n_entries = 1 << 24,
236 .n_entries_ext = 1 << 23,
237 .signature_offset = APP_METADATA_OFFSET(0),
238 .key_offset = APP_METADATA_OFFSET(32),
244 struct rte_pipeline_table_params table_params = {
245 .ops = &rte_table_hash_key8_ext_ops,
246 .arg_create = &table_hash_params,
247 .f_action_hit = NULL,
248 .f_action_miss = NULL,
250 .action_data_size = 0,
253 if (rte_pipeline_table_create(p, &table_params, &table_id))
254 rte_panic("Unable to configure the hash table\n");
258 case e_APP_PIPELINE_HASH_SPEC_KEY8_LRU:
260 struct rte_table_hash_key8_lru_params table_hash_params = {
261 .n_entries = 1 << 24,
262 .signature_offset = APP_METADATA_OFFSET(0),
263 .key_offset = APP_METADATA_OFFSET(32),
269 struct rte_pipeline_table_params table_params = {
270 .ops = &rte_table_hash_key8_lru_ops,
271 .arg_create = &table_hash_params,
272 .f_action_hit = NULL,
273 .f_action_miss = NULL,
275 .action_data_size = 0,
278 if (rte_pipeline_table_create(p, &table_params, &table_id))
279 rte_panic("Unable to configure the hash table\n");
283 case e_APP_PIPELINE_HASH_SPEC_KEY16_EXT:
285 struct rte_table_hash_key16_ext_params table_hash_params = {
286 .n_entries = 1 << 24,
287 .n_entries_ext = 1 << 23,
288 .signature_offset = APP_METADATA_OFFSET(0),
289 .key_offset = APP_METADATA_OFFSET(32),
295 struct rte_pipeline_table_params table_params = {
296 .ops = &rte_table_hash_key16_ext_ops,
297 .arg_create = &table_hash_params,
298 .f_action_hit = NULL,
299 .f_action_miss = NULL,
301 .action_data_size = 0,
304 if (rte_pipeline_table_create(p, &table_params, &table_id))
305 rte_panic("Unable to configure the hash table)\n");
309 case e_APP_PIPELINE_HASH_SPEC_KEY16_LRU:
311 struct rte_table_hash_key16_lru_params table_hash_params = {
312 .n_entries = 1 << 24,
313 .signature_offset = APP_METADATA_OFFSET(0),
314 .key_offset = APP_METADATA_OFFSET(32),
320 struct rte_pipeline_table_params table_params = {
321 .ops = &rte_table_hash_key16_lru_ops,
322 .arg_create = &table_hash_params,
323 .f_action_hit = NULL,
324 .f_action_miss = NULL,
326 .action_data_size = 0,
329 if (rte_pipeline_table_create(p, &table_params, &table_id))
330 rte_panic("Unable to configure the hash table\n");
334 case e_APP_PIPELINE_HASH_SPEC_KEY32_EXT:
336 struct rte_table_hash_key32_ext_params table_hash_params = {
337 .n_entries = 1 << 24,
338 .n_entries_ext = 1 << 23,
339 .signature_offset = APP_METADATA_OFFSET(0),
340 .key_offset = APP_METADATA_OFFSET(32),
345 struct rte_pipeline_table_params table_params = {
346 .ops = &rte_table_hash_key32_ext_ops,
347 .arg_create = &table_hash_params,
348 .f_action_hit = NULL,
349 .f_action_miss = NULL,
351 .action_data_size = 0,
354 if (rte_pipeline_table_create(p, &table_params, &table_id))
355 rte_panic("Unable to configure the hash table\n");
360 case e_APP_PIPELINE_HASH_SPEC_KEY32_LRU:
362 struct rte_table_hash_key32_lru_params table_hash_params = {
363 .n_entries = 1 << 24,
364 .signature_offset = APP_METADATA_OFFSET(0),
365 .key_offset = APP_METADATA_OFFSET(32),
370 struct rte_pipeline_table_params table_params = {
371 .ops = &rte_table_hash_key32_lru_ops,
372 .arg_create = &table_hash_params,
373 .f_action_hit = NULL,
374 .f_action_miss = NULL,
376 .action_data_size = 0,
379 if (rte_pipeline_table_create(p, &table_params, &table_id))
380 rte_panic("Unable to configure the hash table\n");
384 case e_APP_PIPELINE_HASH_CUCKOO_KEY8:
385 case e_APP_PIPELINE_HASH_CUCKOO_KEY16:
386 case e_APP_PIPELINE_HASH_CUCKOO_KEY32:
387 case e_APP_PIPELINE_HASH_CUCKOO_KEY48:
388 case e_APP_PIPELINE_HASH_CUCKOO_KEY64:
389 case e_APP_PIPELINE_HASH_CUCKOO_KEY80:
390 case e_APP_PIPELINE_HASH_CUCKOO_KEY96:
391 case e_APP_PIPELINE_HASH_CUCKOO_KEY112:
392 case e_APP_PIPELINE_HASH_CUCKOO_KEY128:
394 char hash_name[RTE_HASH_NAMESIZE];
396 snprintf(hash_name, sizeof(hash_name), "RTE_TH_CUCKOO_%d",
399 struct rte_table_hash_cuckoo_params table_hash_params = {
400 .key_size = key_size,
401 .n_keys = (1 << 24) + 1,
404 .signature_offset = APP_METADATA_OFFSET(0),
405 .key_offset = APP_METADATA_OFFSET(32),
409 struct rte_pipeline_table_params table_params = {
410 .ops = &rte_table_hash_cuckoo_ops,
411 .arg_create = &table_hash_params,
412 .f_action_hit = NULL,
413 .f_action_miss = NULL,
415 .action_data_size = 0,
418 if (rte_pipeline_table_create(p, &table_params, &table_id))
419 rte_panic("Unable to configure the hash table\n");
424 rte_panic("Invalid hash table type or key size\n");
427 /* Interconnecting ports and tables */
428 for (i = 0; i < app.n_ports; i++)
429 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
431 rte_panic("Unable to connect input port %u to "
432 "table %u\n", port_in_id[i], table_id);
434 /* Add entries to tables */
435 for (i = 0; i < (1 << 24); i++) {
436 struct rte_pipeline_table_entry entry = {
437 .action = RTE_PIPELINE_ACTION_PORT,
438 {.port_id = port_out_id[i & (app.n_ports - 1)]},
440 struct rte_pipeline_table_entry *entry_ptr;
442 uint32_t *k32 = (uint32_t *) key;
443 int key_found, status;
445 memset(key, 0, sizeof(key));
446 k32[0] = rte_be_to_cpu_32(i);
448 status = rte_pipeline_table_entry_add(p, table_id, key, &entry,
449 &key_found, &entry_ptr);
451 rte_panic("Unable to add entry to table %u (%d)\n",
455 /* Enable input ports */
456 for (i = 0; i < app.n_ports; i++)
457 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
458 rte_panic("Unable to enable input port %u\n",
461 /* Check pipeline consistency */
462 if (rte_pipeline_check(p) < 0)
463 rte_panic("Pipeline consistency check failed\n");
473 if ((i & APP_FLUSH) == 0)
474 rte_pipeline_flush(p);
481 __attribute__((unused)) uint32_t key_size,
482 __attribute__((unused)) uint64_t seed)
485 uint32_t ip_dst = rte_be_to_cpu_32(k32[0]);
486 uint64_t signature = (ip_dst >> 2) | ((ip_dst & 0x3) << 30);
492 app_main_loop_rx_metadata(void) {
496 RTE_LOG(INFO, USER1, "Core %u is doing RX (with meta-data)\n",
499 for (i = 0; ; i = ((i + 1) & (app.n_ports - 1))) {
502 n_mbufs = rte_eth_rx_burst(
506 app.burst_size_rx_read);
511 for (j = 0; j < n_mbufs; j++) {
513 uint8_t *m_data, *key;
514 struct ipv4_hdr *ip_hdr;
515 struct ipv6_hdr *ipv6_hdr;
518 uint32_t *signature, *k32;
520 m = app.mbuf_rx.array[j];
521 m_data = rte_pktmbuf_mtod(m, uint8_t *);
522 signature = RTE_MBUF_METADATA_UINT32_PTR(m,
523 APP_METADATA_OFFSET(0));
524 key = RTE_MBUF_METADATA_UINT8_PTR(m,
525 APP_METADATA_OFFSET(32));
527 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
528 ip_hdr = (struct ipv4_hdr *)
529 &m_data[sizeof(struct ether_hdr)];
530 ip_dst = ip_hdr->dst_addr;
532 k32 = (uint32_t *) key;
533 k32[0] = ip_dst & 0xFFFFFF00;
534 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
535 ipv6_hdr = (struct ipv6_hdr *)
536 &m_data[sizeof(struct ether_hdr)];
537 ipv6_dst = ipv6_hdr->dst_addr;
539 memcpy(key, ipv6_dst, 16);
543 *signature = test_hash(key, 0, 0);
547 ret = rte_ring_sp_enqueue_bulk(
549 (void **) app.mbuf_rx.array,