table: rework variable size key lru hash table
[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_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),
239                         .key_mask = NULL,
240                         .f_hash = test_hash,
241                         .seed = 0,
242                 };
243
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,
249                         .arg_ah = NULL,
250                         .action_data_size = 0,
251                 };
252
253                 if (rte_pipeline_table_create(p, &table_params, &table_id))
254                         rte_panic("Unable to configure the hash table\n");
255         }
256         break;
257
258         case e_APP_PIPELINE_HASH_SPEC_KEY8_LRU:
259         {
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),
264                         .key_mask = NULL,
265                         .f_hash = test_hash,
266                         .seed = 0,
267                 };
268
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,
274                         .arg_ah = NULL,
275                         .action_data_size = 0,
276                 };
277
278                 if (rte_pipeline_table_create(p, &table_params, &table_id))
279                         rte_panic("Unable to configure the hash table\n");
280         }
281         break;
282
283         case e_APP_PIPELINE_HASH_SPEC_KEY16_EXT:
284         {
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),
290                         .f_hash = test_hash,
291                         .seed = 0,
292                         .key_mask = NULL,
293                 };
294
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,
300                         .arg_ah = NULL,
301                         .action_data_size = 0,
302                 };
303
304                 if (rte_pipeline_table_create(p, &table_params, &table_id))
305                         rte_panic("Unable to configure the hash table)\n");
306         }
307         break;
308
309         case e_APP_PIPELINE_HASH_SPEC_KEY16_LRU:
310         {
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),
315                         .f_hash = test_hash,
316                         .seed = 0,
317                         .key_mask = NULL,
318                 };
319
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,
325                         .arg_ah = NULL,
326                         .action_data_size = 0,
327                 };
328
329                 if (rte_pipeline_table_create(p, &table_params, &table_id))
330                         rte_panic("Unable to configure the hash table\n");
331         }
332         break;
333
334         case e_APP_PIPELINE_HASH_SPEC_KEY32_EXT:
335         {
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),
341                         .f_hash = test_hash,
342                         .seed = 0,
343                 };
344
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,
350                         .arg_ah = NULL,
351                         .action_data_size = 0,
352                 };
353
354                 if (rte_pipeline_table_create(p, &table_params, &table_id))
355                         rte_panic("Unable to configure the hash table\n");
356         }
357         break;
358
359
360         case e_APP_PIPELINE_HASH_SPEC_KEY32_LRU:
361         {
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),
366                         .f_hash = test_hash,
367                         .seed = 0,
368                 };
369
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,
375                         .arg_ah = NULL,
376                         .action_data_size = 0,
377                 };
378
379                 if (rte_pipeline_table_create(p, &table_params, &table_id))
380                         rte_panic("Unable to configure the hash table\n");
381         }
382         break;
383
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:
393         {
394                 char hash_name[RTE_HASH_NAMESIZE];
395
396                 snprintf(hash_name, sizeof(hash_name), "RTE_TH_CUCKOO_%d",
397                         app.pipeline_type);
398
399                 struct rte_table_hash_cuckoo_params table_hash_params = {
400                         .key_size = key_size,
401                         .n_keys = (1 << 24) + 1,
402                         .f_hash = test_hash,
403                         .seed = 0,
404                         .signature_offset = APP_METADATA_OFFSET(0),
405                         .key_offset = APP_METADATA_OFFSET(32),
406                         .name = hash_name,
407                 };
408
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,
414                         .arg_ah = NULL,
415                         .action_data_size = 0,
416                 };
417
418                 if (rte_pipeline_table_create(p, &table_params, &table_id))
419                         rte_panic("Unable to configure the hash table\n");
420         }
421         break;
422
423         default:
424                 rte_panic("Invalid hash table type or key size\n");
425         }
426
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],
430                         table_id))
431                         rte_panic("Unable to connect input port %u to "
432                                 "table %u\n", port_in_id[i],  table_id);
433
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)]},
439                 };
440                 struct rte_pipeline_table_entry *entry_ptr;
441                 uint8_t key[32];
442                 uint32_t *k32 = (uint32_t *) key;
443                 int key_found, status;
444
445                 memset(key, 0, sizeof(key));
446                 k32[0] = rte_be_to_cpu_32(i);
447
448                 status = rte_pipeline_table_entry_add(p, table_id, key, &entry,
449                         &key_found, &entry_ptr);
450                 if (status < 0)
451                         rte_panic("Unable to add entry to table %u (%d)\n",
452                                 table_id, status);
453         }
454
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",
459                                 port_in_id[i]);
460
461         /* Check pipeline consistency */
462         if (rte_pipeline_check(p) < 0)
463                 rte_panic("Pipeline consistency check failed\n");
464
465         /* Run-time */
466 #if APP_FLUSH == 0
467         for ( ; ; )
468                 rte_pipeline_run(p);
469 #else
470         for (i = 0; ; i++) {
471                 rte_pipeline_run(p);
472
473                 if ((i & APP_FLUSH) == 0)
474                         rte_pipeline_flush(p);
475         }
476 #endif
477 }
478
479 uint64_t test_hash(
480         void *key,
481         __attribute__((unused)) uint32_t key_size,
482         __attribute__((unused)) uint64_t seed)
483 {
484         uint32_t *k32 = key;
485         uint32_t ip_dst = rte_be_to_cpu_32(k32[0]);
486         uint64_t signature = (ip_dst >> 2) | ((ip_dst & 0x3) << 30);
487
488         return signature;
489 }
490
491 void
492 app_main_loop_rx_metadata(void) {
493         uint32_t i, j;
494         int ret;
495
496         RTE_LOG(INFO, USER1, "Core %u is doing RX (with meta-data)\n",
497                 rte_lcore_id());
498
499         for (i = 0; ; i = ((i + 1) & (app.n_ports - 1))) {
500                 uint16_t n_mbufs;
501
502                 n_mbufs = rte_eth_rx_burst(
503                         app.ports[i],
504                         0,
505                         app.mbuf_rx.array,
506                         app.burst_size_rx_read);
507
508                 if (n_mbufs == 0)
509                         continue;
510
511                 for (j = 0; j < n_mbufs; j++) {
512                         struct rte_mbuf *m;
513                         uint8_t *m_data, *key;
514                         struct ipv4_hdr *ip_hdr;
515                         struct ipv6_hdr *ipv6_hdr;
516                         uint32_t ip_dst;
517                         uint8_t *ipv6_dst;
518                         uint32_t *signature, *k32;
519
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));
526
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;
531
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;
538
539                                 memcpy(key, ipv6_dst, 16);
540                         } else
541                                 continue;
542
543                         *signature = test_hash(key, 0, 0);
544                 }
545
546                 do {
547                         ret = rte_ring_sp_enqueue_bulk(
548                                 app.rings_rx[i],
549                                 (void **) app.mbuf_rx.array,
550                                 n_mbufs,
551                                 NULL);
552                 } while (ret == 0);
553         }
554 }