1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
10 #include <rte_ethdev.h>
11 #include <rte_ether.h>
13 #include <rte_byteorder.h>
15 #include <rte_port_ring.h>
16 #include <rte_table_hash.h>
18 #include <rte_table_hash_cuckoo.h>
19 #include <rte_pipeline.h>
24 translate_options(uint32_t *special, uint32_t *ext, uint32_t *key_size)
26 switch (app.pipeline_type) {
27 case e_APP_PIPELINE_HASH_KEY8_EXT:
28 *special = 0; *ext = 1; *key_size = 8; return;
29 case e_APP_PIPELINE_HASH_KEY8_LRU:
30 *special = 0; *ext = 0; *key_size = 8; return;
31 case e_APP_PIPELINE_HASH_KEY16_EXT:
32 *special = 0; *ext = 1; *key_size = 16; return;
33 case e_APP_PIPELINE_HASH_KEY16_LRU:
34 *special = 0; *ext = 0; *key_size = 16; return;
35 case e_APP_PIPELINE_HASH_KEY32_EXT:
36 *special = 0; *ext = 1; *key_size = 32; return;
37 case e_APP_PIPELINE_HASH_KEY32_LRU:
38 *special = 0; *ext = 0; *key_size = 32; return;
40 case e_APP_PIPELINE_HASH_SPEC_KEY8_EXT:
41 *special = 1; *ext = 1; *key_size = 8; return;
42 case e_APP_PIPELINE_HASH_SPEC_KEY8_LRU:
43 *special = 1; *ext = 0; *key_size = 8; return;
44 case e_APP_PIPELINE_HASH_SPEC_KEY16_EXT:
45 *special = 1; *ext = 1; *key_size = 16; return;
46 case e_APP_PIPELINE_HASH_SPEC_KEY16_LRU:
47 *special = 1; *ext = 0; *key_size = 16; return;
48 case e_APP_PIPELINE_HASH_SPEC_KEY32_EXT:
49 *special = 1; *ext = 1; *key_size = 32; return;
50 case e_APP_PIPELINE_HASH_SPEC_KEY32_LRU:
51 *special = 1; *ext = 0; *key_size = 32; return;
53 case e_APP_PIPELINE_HASH_CUCKOO_KEY8:
54 *special = 0; *ext = 0; *key_size = 8; return;
55 case e_APP_PIPELINE_HASH_CUCKOO_KEY16:
56 *special = 0; *ext = 0; *key_size = 16; return;
57 case e_APP_PIPELINE_HASH_CUCKOO_KEY32:
58 *special = 0; *ext = 0; *key_size = 32; return;
59 case e_APP_PIPELINE_HASH_CUCKOO_KEY48:
60 *special = 0; *ext = 0; *key_size = 48; return;
61 case e_APP_PIPELINE_HASH_CUCKOO_KEY64:
62 *special = 0; *ext = 0; *key_size = 64; return;
63 case e_APP_PIPELINE_HASH_CUCKOO_KEY80:
64 *special = 0; *ext = 0; *key_size = 80; return;
65 case e_APP_PIPELINE_HASH_CUCKOO_KEY96:
66 *special = 0; *ext = 0; *key_size = 96; return;
67 case e_APP_PIPELINE_HASH_CUCKOO_KEY112:
68 *special = 0; *ext = 0; *key_size = 112; return;
69 case e_APP_PIPELINE_HASH_CUCKOO_KEY128:
70 *special = 0; *ext = 0; *key_size = 128; return;
73 rte_panic("Invalid hash table type or key size\n");
77 app_main_loop_worker_pipeline_hash(void) {
78 struct rte_pipeline_params pipeline_params = {
80 .socket_id = rte_socket_id(),
83 struct rte_pipeline *p;
84 uint32_t port_in_id[APP_MAX_PORTS];
85 uint32_t port_out_id[APP_MAX_PORTS];
88 uint32_t special, ext, key_size;
90 translate_options(&special, &ext, &key_size);
92 RTE_LOG(INFO, USER1, "Core %u is doing work "
93 "(pipeline with hash table, %s, %s, %d-byte key)\n",
95 special ? "specialized" : "non-specialized",
96 ext ? "extendible bucket" : "LRU",
99 /* Pipeline configuration */
100 p = rte_pipeline_create(&pipeline_params);
102 rte_panic("Unable to configure the pipeline\n");
104 /* Input port configuration */
105 for (i = 0; i < app.n_ports; i++) {
106 struct rte_port_ring_reader_params port_ring_params = {
107 .ring = app.rings_rx[i],
110 struct rte_pipeline_port_in_params port_params = {
111 .ops = &rte_port_ring_reader_ops,
112 .arg_create = (void *) &port_ring_params,
115 .burst_size = app.burst_size_worker_read,
118 if (rte_pipeline_port_in_create(p, &port_params,
120 rte_panic("Unable to configure input port for "
124 /* Output port configuration */
125 for (i = 0; i < app.n_ports; i++) {
126 struct rte_port_ring_writer_params port_ring_params = {
127 .ring = app.rings_tx[i],
128 .tx_burst_sz = app.burst_size_worker_write,
131 struct rte_pipeline_port_out_params port_params = {
132 .ops = &rte_port_ring_writer_ops,
133 .arg_create = (void *) &port_ring_params,
138 if (rte_pipeline_port_out_create(p, &port_params,
140 rte_panic("Unable to configure output port for "
144 struct rte_table_hash_params table_hash_params = {
146 .key_size = key_size,
147 .key_offset = APP_METADATA_OFFSET(32),
150 .n_buckets = 1 << 22,
155 struct rte_table_hash_cuckoo_params table_hash_cuckoo_params = {
157 .key_size = key_size,
158 .key_offset = APP_METADATA_OFFSET(32),
161 .n_buckets = 1 << 22,
162 .f_hash = test_hash_cuckoo,
166 /* Table configuration */
167 switch (app.pipeline_type) {
168 case e_APP_PIPELINE_HASH_KEY8_EXT:
169 case e_APP_PIPELINE_HASH_KEY16_EXT:
170 case e_APP_PIPELINE_HASH_KEY32_EXT:
172 struct rte_pipeline_table_params table_params = {
173 .ops = &rte_table_hash_ext_ops,
174 .arg_create = &table_hash_params,
175 .f_action_hit = NULL,
176 .f_action_miss = NULL,
178 .action_data_size = 0,
181 if (rte_pipeline_table_create(p, &table_params, &table_id))
182 rte_panic("Unable to configure the hash table\n");
186 case e_APP_PIPELINE_HASH_KEY8_LRU:
187 case e_APP_PIPELINE_HASH_KEY16_LRU:
188 case e_APP_PIPELINE_HASH_KEY32_LRU:
190 struct rte_pipeline_table_params table_params = {
191 .ops = &rte_table_hash_lru_ops,
192 .arg_create = &table_hash_params,
193 .f_action_hit = NULL,
194 .f_action_miss = NULL,
196 .action_data_size = 0,
199 if (rte_pipeline_table_create(p, &table_params, &table_id))
200 rte_panic("Unable to configure the hash table\n");
204 case e_APP_PIPELINE_HASH_SPEC_KEY8_EXT:
206 struct rte_pipeline_table_params table_params = {
207 .ops = &rte_table_hash_key8_ext_ops,
208 .arg_create = &table_hash_params,
209 .f_action_hit = NULL,
210 .f_action_miss = NULL,
212 .action_data_size = 0,
215 if (rte_pipeline_table_create(p, &table_params, &table_id))
216 rte_panic("Unable to configure the hash table\n");
220 case e_APP_PIPELINE_HASH_SPEC_KEY8_LRU:
222 struct rte_pipeline_table_params table_params = {
223 .ops = &rte_table_hash_key8_lru_ops,
224 .arg_create = &table_hash_params,
225 .f_action_hit = NULL,
226 .f_action_miss = NULL,
228 .action_data_size = 0,
231 if (rte_pipeline_table_create(p, &table_params, &table_id))
232 rte_panic("Unable to configure the hash table\n");
236 case e_APP_PIPELINE_HASH_SPEC_KEY16_EXT:
238 struct rte_pipeline_table_params table_params = {
239 .ops = &rte_table_hash_key16_ext_ops,
240 .arg_create = &table_hash_params,
241 .f_action_hit = NULL,
242 .f_action_miss = NULL,
244 .action_data_size = 0,
247 if (rte_pipeline_table_create(p, &table_params, &table_id))
248 rte_panic("Unable to configure the hash table)\n");
252 case e_APP_PIPELINE_HASH_SPEC_KEY16_LRU:
254 struct rte_pipeline_table_params table_params = {
255 .ops = &rte_table_hash_key16_lru_ops,
256 .arg_create = &table_hash_params,
257 .f_action_hit = NULL,
258 .f_action_miss = NULL,
260 .action_data_size = 0,
263 if (rte_pipeline_table_create(p, &table_params, &table_id))
264 rte_panic("Unable to configure the hash table\n");
268 case e_APP_PIPELINE_HASH_SPEC_KEY32_EXT:
270 struct rte_pipeline_table_params table_params = {
271 .ops = &rte_table_hash_key32_ext_ops,
272 .arg_create = &table_hash_params,
273 .f_action_hit = NULL,
274 .f_action_miss = NULL,
276 .action_data_size = 0,
279 if (rte_pipeline_table_create(p, &table_params, &table_id))
280 rte_panic("Unable to configure the hash table\n");
285 case e_APP_PIPELINE_HASH_SPEC_KEY32_LRU:
287 struct rte_pipeline_table_params table_params = {
288 .ops = &rte_table_hash_key32_lru_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");
301 case e_APP_PIPELINE_HASH_CUCKOO_KEY8:
302 case e_APP_PIPELINE_HASH_CUCKOO_KEY16:
303 case e_APP_PIPELINE_HASH_CUCKOO_KEY32:
304 case e_APP_PIPELINE_HASH_CUCKOO_KEY48:
305 case e_APP_PIPELINE_HASH_CUCKOO_KEY64:
306 case e_APP_PIPELINE_HASH_CUCKOO_KEY80:
307 case e_APP_PIPELINE_HASH_CUCKOO_KEY96:
308 case e_APP_PIPELINE_HASH_CUCKOO_KEY112:
309 case e_APP_PIPELINE_HASH_CUCKOO_KEY128:
311 struct rte_pipeline_table_params table_params = {
312 .ops = &rte_table_hash_cuckoo_ops,
313 .arg_create = &table_hash_cuckoo_params,
314 .f_action_hit = NULL,
315 .f_action_miss = NULL,
317 .action_data_size = 0,
320 if (rte_pipeline_table_create(p, &table_params, &table_id))
321 rte_panic("Unable to configure the hash table\n");
326 rte_panic("Invalid hash table type or key size\n");
329 /* Interconnecting ports and tables */
330 for (i = 0; i < app.n_ports; i++)
331 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
333 rte_panic("Unable to connect input port %u to "
334 "table %u\n", port_in_id[i], table_id);
336 /* Add entries to tables */
337 for (i = 0; i < (1 << 24); i++) {
338 struct rte_pipeline_table_entry entry = {
339 .action = RTE_PIPELINE_ACTION_PORT,
340 {.port_id = port_out_id[i & (app.n_ports - 1)]},
342 struct rte_pipeline_table_entry *entry_ptr;
344 uint32_t *k32 = (uint32_t *) key;
345 int key_found, status;
347 memset(key, 0, sizeof(key));
348 k32[0] = rte_be_to_cpu_32(i);
350 status = rte_pipeline_table_entry_add(p, table_id, key, &entry,
351 &key_found, &entry_ptr);
353 rte_panic("Unable to add entry to table %u (%d)\n",
357 /* Enable input ports */
358 for (i = 0; i < app.n_ports; i++)
359 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
360 rte_panic("Unable to enable input port %u\n",
363 /* Check pipeline consistency */
364 if (rte_pipeline_check(p) < 0)
365 rte_panic("Pipeline consistency check failed\n");
375 if ((i & APP_FLUSH) == 0)
376 rte_pipeline_flush(p);
383 __rte_unused void *key_mask,
384 __rte_unused uint32_t key_size,
385 __rte_unused uint64_t seed)
388 uint32_t ip_dst = rte_be_to_cpu_32(k32[0]);
389 uint64_t signature = (ip_dst >> 2) | ((ip_dst & 0x3) << 30);
394 uint32_t test_hash_cuckoo(
396 __rte_unused uint32_t key_size,
397 __rte_unused uint32_t seed)
399 const uint32_t *k32 = key;
400 uint32_t ip_dst = rte_be_to_cpu_32(k32[0]);
401 uint32_t signature = (ip_dst >> 2) | ((ip_dst & 0x3) << 30);
407 app_main_loop_rx_metadata(void) {
411 RTE_LOG(INFO, USER1, "Core %u is doing RX (with meta-data)\n",
414 for (i = 0; ; i = ((i + 1) & (app.n_ports - 1))) {
417 n_mbufs = rte_eth_rx_burst(
421 app.burst_size_rx_read);
426 for (j = 0; j < n_mbufs; j++) {
428 uint8_t *m_data, *key;
429 struct rte_ipv4_hdr *ip_hdr;
430 struct rte_ipv6_hdr *ipv6_hdr;
433 uint32_t *signature, *k32;
435 m = app.mbuf_rx.array[j];
436 m_data = rte_pktmbuf_mtod(m, uint8_t *);
437 signature = RTE_MBUF_METADATA_UINT32_PTR(m,
438 APP_METADATA_OFFSET(0));
439 key = RTE_MBUF_METADATA_UINT8_PTR(m,
440 APP_METADATA_OFFSET(32));
442 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
443 ip_hdr = (struct rte_ipv4_hdr *)
444 &m_data[sizeof(struct rte_ether_hdr)];
445 ip_dst = ip_hdr->dst_addr;
447 k32 = (uint32_t *) key;
448 k32[0] = ip_dst & 0xFFFFFF00;
449 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
450 ipv6_hdr = (struct rte_ipv6_hdr *)
451 &m_data[sizeof(struct rte_ether_hdr)];
452 ipv6_dst = ipv6_hdr->dst_addr;
454 memcpy(key, ipv6_dst, 16);
458 *signature = test_hash(key, NULL, 0, 0);
462 ret = rte_ring_sp_enqueue_bulk(
464 (void **) app.mbuf_rx.array,