table: rework 32-byte key hash tables
[dpdk.git] / test / test-pipeline / pipeline_hash.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 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 <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37
38 #include <rte_log.h>
39 #include <rte_ethdev.h>
40 #include <rte_ether.h>
41 #include <rte_ip.h>
42 #include <rte_byteorder.h>
43
44 #include <rte_port_ring.h>
45 #include <rte_table_hash.h>
46 #include <rte_hash.h>
47 #include <rte_pipeline.h>
48
49 #include "main.h"
50
51 static void
52 translate_options(uint32_t *special, uint32_t *ext, uint32_t *key_size)
53 {
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;
67
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;
80
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;
99
100         default:
101                 rte_panic("Invalid hash table type or key size\n");
102         }
103 }
104 void
105 app_main_loop_worker_pipeline_hash(void) {
106         struct rte_pipeline_params pipeline_params = {
107                 .name = "pipeline",
108                 .socket_id = rte_socket_id(),
109         };
110
111         struct rte_pipeline *p;
112         uint32_t port_in_id[APP_MAX_PORTS];
113         uint32_t port_out_id[APP_MAX_PORTS];
114         uint32_t table_id;
115         uint32_t i;
116         uint32_t special, ext, key_size;
117
118         translate_options(&special, &ext, &key_size);
119
120         RTE_LOG(INFO, USER1, "Core %u is doing work "
121                 "(pipeline with hash table, %s, %s, %d-byte key)\n",
122                 rte_lcore_id(),
123                 special ? "specialized" : "non-specialized",
124                 ext ? "extendible bucket" : "LRU",
125                 key_size);
126
127         /* Pipeline configuration */
128         p = rte_pipeline_create(&pipeline_params);
129         if (p == NULL)
130                 rte_panic("Unable to configure the pipeline\n");
131
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],
136                 };
137
138                 struct rte_pipeline_port_in_params port_params = {
139                         .ops = &rte_port_ring_reader_ops,
140                         .arg_create = (void *) &port_ring_params,
141                         .f_action = NULL,
142                         .arg_ah = NULL,
143                         .burst_size = app.burst_size_worker_read,
144                 };
145
146                 if (rte_pipeline_port_in_create(p, &port_params,
147                         &port_in_id[i]))
148                         rte_panic("Unable to configure input port for "
149                                 "ring %d\n", i);
150         }
151
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,
157                 };
158
159                 struct rte_pipeline_port_out_params port_params = {
160                         .ops = &rte_port_ring_writer_ops,
161                         .arg_create = (void *) &port_ring_params,
162                         .f_action = NULL,
163                         .arg_ah = NULL,
164                 };
165
166                 if (rte_pipeline_port_out_create(p, &port_params,
167                         &port_out_id[i]))
168                         rte_panic("Unable to configure output port for "
169                                 "ring %d\n", i);
170         }
171
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:
177         {
178                 struct rte_table_hash_params table_hash_params = {
179                         .name = "TABLE",
180                         .key_size = key_size,
181                         .key_offset = APP_METADATA_OFFSET(32),
182                         .key_mask = NULL,
183                         .n_keys = 1 << 24,
184                         .n_buckets = 1 << 22,
185                         .f_hash = (rte_table_hash_op_hash)test_hash,
186                         .seed = 0,
187                 };
188
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,
194                         .arg_ah = NULL,
195                         .action_data_size = 0,
196                 };
197
198                 if (rte_pipeline_table_create(p, &table_params, &table_id))
199                         rte_panic("Unable to configure the hash table\n");
200         }
201         break;
202
203         case e_APP_PIPELINE_HASH_KEY8_LRU:
204         case e_APP_PIPELINE_HASH_KEY16_LRU:
205         case e_APP_PIPELINE_HASH_KEY32_LRU:
206         {
207                 struct rte_table_hash_params table_hash_params = {
208                         .name = "TABLE",
209                         .key_size = key_size,
210                         .key_offset = APP_METADATA_OFFSET(32),
211                         .key_mask = NULL,
212                         .n_keys = 1 << 24,
213                         .n_buckets = 1 << 22,
214                         .f_hash = (rte_table_hash_op_hash)test_hash,
215                         .seed = 0,
216                 };
217
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,
223                         .arg_ah = NULL,
224                         .action_data_size = 0,
225                 };
226
227                 if (rte_pipeline_table_create(p, &table_params, &table_id))
228                         rte_panic("Unable to configure the hash table\n");
229         }
230         break;
231
232         case e_APP_PIPELINE_HASH_SPEC_KEY8_EXT:
233         {
234                 struct rte_table_hash_params table_hash_params = {
235                         .name = "TABLE",
236                         .key_size = key_size,
237                         .key_offset = APP_METADATA_OFFSET(32),
238                         .key_mask = NULL,
239                         .n_keys = 1 << 24,
240                         .n_buckets = 1 << 22,
241                         .f_hash = (rte_table_hash_op_hash)test_hash,
242                         .seed = 0,
243                 };
244
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,
250                         .arg_ah = NULL,
251                         .action_data_size = 0,
252                 };
253
254                 if (rte_pipeline_table_create(p, &table_params, &table_id))
255                         rte_panic("Unable to configure the hash table\n");
256         }
257         break;
258
259         case e_APP_PIPELINE_HASH_SPEC_KEY8_LRU:
260         {
261                 struct rte_table_hash_params table_hash_params = {
262                         .name = "TABLE",
263                         .key_size = key_size,
264                         .key_offset = APP_METADATA_OFFSET(32),
265                         .key_mask = NULL,
266                         .n_keys = 1 << 24,
267                         .n_buckets = 1 << 22,
268                         .f_hash = (rte_table_hash_op_hash)test_hash,
269                         .seed = 0,
270                 };
271
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,
277                         .arg_ah = NULL,
278                         .action_data_size = 0,
279                 };
280
281                 if (rte_pipeline_table_create(p, &table_params, &table_id))
282                         rte_panic("Unable to configure the hash table\n");
283         }
284         break;
285
286         case e_APP_PIPELINE_HASH_SPEC_KEY16_EXT:
287         {
288                 struct rte_table_hash_params table_hash_params = {
289                         .name = "TABLE",
290                         .key_size = key_size,
291                         .key_offset = APP_METADATA_OFFSET(32),
292                         .key_mask = NULL,
293                         .n_keys = 1 << 24,
294                         .n_buckets = 1 << 22,
295                         .f_hash = (rte_table_hash_op_hash)test_hash,
296                         .seed = 0,
297                 };
298
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,
304                         .arg_ah = NULL,
305                         .action_data_size = 0,
306                 };
307
308                 if (rte_pipeline_table_create(p, &table_params, &table_id))
309                         rte_panic("Unable to configure the hash table)\n");
310         }
311         break;
312
313         case e_APP_PIPELINE_HASH_SPEC_KEY16_LRU:
314         {
315                 struct rte_table_hash_params table_hash_params = {
316                         .name = "TABLE",
317                         .key_size = key_size,
318                         .key_offset = APP_METADATA_OFFSET(32),
319                         .key_mask = NULL,
320                         .n_keys = 1 << 24,
321                         .n_buckets = 1 << 22,
322                         .f_hash = (rte_table_hash_op_hash)test_hash,
323                         .seed = 0,
324                 };
325
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,
331                         .arg_ah = NULL,
332                         .action_data_size = 0,
333                 };
334
335                 if (rte_pipeline_table_create(p, &table_params, &table_id))
336                         rte_panic("Unable to configure the hash table\n");
337         }
338         break;
339
340         case e_APP_PIPELINE_HASH_SPEC_KEY32_EXT:
341         {
342                 struct rte_table_hash_params table_hash_params = {
343                         .name = "TABLE",
344                         .key_size = key_size,
345                         .key_offset = APP_METADATA_OFFSET(32),
346                         .key_mask = NULL,
347                         .n_keys = 1 << 24,
348                         .n_buckets = 1 << 22,
349                         .f_hash = (rte_table_hash_op_hash)test_hash,
350                         .seed = 0,
351                 };
352
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,
358                         .arg_ah = NULL,
359                         .action_data_size = 0,
360                 };
361
362                 if (rte_pipeline_table_create(p, &table_params, &table_id))
363                         rte_panic("Unable to configure the hash table\n");
364         }
365         break;
366
367
368         case e_APP_PIPELINE_HASH_SPEC_KEY32_LRU:
369         {
370                 struct rte_table_hash_params table_hash_params = {
371                         .name = "TABLE",
372                         .key_size = key_size,
373                         .key_offset = APP_METADATA_OFFSET(32),
374                         .key_mask = NULL,
375                         .n_keys = 1 << 24,
376                         .n_buckets = 1 << 22,
377                         .f_hash = (rte_table_hash_op_hash)test_hash,
378                         .seed = 0,
379                 };
380
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,
386                         .arg_ah = NULL,
387                         .action_data_size = 0,
388                 };
389
390                 if (rte_pipeline_table_create(p, &table_params, &table_id))
391                         rte_panic("Unable to configure the hash table\n");
392         }
393         break;
394
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:
404         {
405                 char hash_name[RTE_HASH_NAMESIZE];
406
407                 snprintf(hash_name, sizeof(hash_name), "RTE_TH_CUCKOO_%d",
408                         app.pipeline_type);
409
410                 struct rte_table_hash_cuckoo_params table_hash_params = {
411                         .key_size = key_size,
412                         .n_keys = (1 << 24) + 1,
413                         .f_hash = test_hash,
414                         .seed = 0,
415                         .signature_offset = APP_METADATA_OFFSET(0),
416                         .key_offset = APP_METADATA_OFFSET(32),
417                         .name = hash_name,
418                 };
419
420                 struct rte_pipeline_table_params table_params = {
421                         .ops = &rte_table_hash_cuckoo_ops,
422                         .arg_create = &table_hash_params,
423                         .f_action_hit = NULL,
424                         .f_action_miss = NULL,
425                         .arg_ah = NULL,
426                         .action_data_size = 0,
427                 };
428
429                 if (rte_pipeline_table_create(p, &table_params, &table_id))
430                         rte_panic("Unable to configure the hash table\n");
431         }
432         break;
433
434         default:
435                 rte_panic("Invalid hash table type or key size\n");
436         }
437
438         /* Interconnecting ports and tables */
439         for (i = 0; i < app.n_ports; i++)
440                 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
441                         table_id))
442                         rte_panic("Unable to connect input port %u to "
443                                 "table %u\n", port_in_id[i],  table_id);
444
445         /* Add entries to tables */
446         for (i = 0; i < (1 << 24); i++) {
447                 struct rte_pipeline_table_entry entry = {
448                         .action = RTE_PIPELINE_ACTION_PORT,
449                         {.port_id = port_out_id[i & (app.n_ports - 1)]},
450                 };
451                 struct rte_pipeline_table_entry *entry_ptr;
452                 uint8_t key[32];
453                 uint32_t *k32 = (uint32_t *) key;
454                 int key_found, status;
455
456                 memset(key, 0, sizeof(key));
457                 k32[0] = rte_be_to_cpu_32(i);
458
459                 status = rte_pipeline_table_entry_add(p, table_id, key, &entry,
460                         &key_found, &entry_ptr);
461                 if (status < 0)
462                         rte_panic("Unable to add entry to table %u (%d)\n",
463                                 table_id, status);
464         }
465
466         /* Enable input ports */
467         for (i = 0; i < app.n_ports; i++)
468                 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
469                         rte_panic("Unable to enable input port %u\n",
470                                 port_in_id[i]);
471
472         /* Check pipeline consistency */
473         if (rte_pipeline_check(p) < 0)
474                 rte_panic("Pipeline consistency check failed\n");
475
476         /* Run-time */
477 #if APP_FLUSH == 0
478         for ( ; ; )
479                 rte_pipeline_run(p);
480 #else
481         for (i = 0; ; i++) {
482                 rte_pipeline_run(p);
483
484                 if ((i & APP_FLUSH) == 0)
485                         rte_pipeline_flush(p);
486         }
487 #endif
488 }
489
490 uint64_t test_hash(
491         void *key,
492         __attribute__((unused)) uint32_t key_size,
493         __attribute__((unused)) uint64_t seed)
494 {
495         uint32_t *k32 = key;
496         uint32_t ip_dst = rte_be_to_cpu_32(k32[0]);
497         uint64_t signature = (ip_dst >> 2) | ((ip_dst & 0x3) << 30);
498
499         return signature;
500 }
501
502 void
503 app_main_loop_rx_metadata(void) {
504         uint32_t i, j;
505         int ret;
506
507         RTE_LOG(INFO, USER1, "Core %u is doing RX (with meta-data)\n",
508                 rte_lcore_id());
509
510         for (i = 0; ; i = ((i + 1) & (app.n_ports - 1))) {
511                 uint16_t n_mbufs;
512
513                 n_mbufs = rte_eth_rx_burst(
514                         app.ports[i],
515                         0,
516                         app.mbuf_rx.array,
517                         app.burst_size_rx_read);
518
519                 if (n_mbufs == 0)
520                         continue;
521
522                 for (j = 0; j < n_mbufs; j++) {
523                         struct rte_mbuf *m;
524                         uint8_t *m_data, *key;
525                         struct ipv4_hdr *ip_hdr;
526                         struct ipv6_hdr *ipv6_hdr;
527                         uint32_t ip_dst;
528                         uint8_t *ipv6_dst;
529                         uint32_t *signature, *k32;
530
531                         m = app.mbuf_rx.array[j];
532                         m_data = rte_pktmbuf_mtod(m, uint8_t *);
533                         signature = RTE_MBUF_METADATA_UINT32_PTR(m,
534                                         APP_METADATA_OFFSET(0));
535                         key = RTE_MBUF_METADATA_UINT8_PTR(m,
536                                         APP_METADATA_OFFSET(32));
537
538                         if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
539                                 ip_hdr = (struct ipv4_hdr *)
540                                         &m_data[sizeof(struct ether_hdr)];
541                                 ip_dst = ip_hdr->dst_addr;
542
543                                 k32 = (uint32_t *) key;
544                                 k32[0] = ip_dst & 0xFFFFFF00;
545                         } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
546                                 ipv6_hdr = (struct ipv6_hdr *)
547                                         &m_data[sizeof(struct ether_hdr)];
548                                 ipv6_dst = ipv6_hdr->dst_addr;
549
550                                 memcpy(key, ipv6_dst, 16);
551                         } else
552                                 continue;
553
554                         *signature = test_hash(key, 0, 0);
555                 }
556
557                 do {
558                         ret = rte_ring_sp_enqueue_bulk(
559                                 app.rings_rx[i],
560                                 (void **) app.mbuf_rx.array,
561                                 n_mbufs,
562                                 NULL);
563                 } while (ret == 0);
564         }
565 }