222c48c9deb1a59252ef394145e53f884a51da21
[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_key32_ext_params table_hash_params = {
343                         .n_entries = 1 << 24,
344                         .n_entries_ext = 1 << 23,
345                         .signature_offset = APP_METADATA_OFFSET(0),
346                         .key_offset = APP_METADATA_OFFSET(32),
347                         .f_hash = test_hash,
348                         .seed = 0,
349                 };
350
351                 struct rte_pipeline_table_params table_params = {
352                         .ops = &rte_table_hash_key32_ext_ops,
353                         .arg_create = &table_hash_params,
354                         .f_action_hit = NULL,
355                         .f_action_miss = NULL,
356                         .arg_ah = NULL,
357                         .action_data_size = 0,
358                 };
359
360                 if (rte_pipeline_table_create(p, &table_params, &table_id))
361                         rte_panic("Unable to configure the hash table\n");
362         }
363         break;
364
365
366         case e_APP_PIPELINE_HASH_SPEC_KEY32_LRU:
367         {
368                 struct rte_table_hash_key32_lru_params table_hash_params = {
369                         .n_entries = 1 << 24,
370                         .signature_offset = APP_METADATA_OFFSET(0),
371                         .key_offset = APP_METADATA_OFFSET(32),
372                         .f_hash = test_hash,
373                         .seed = 0,
374                 };
375
376                 struct rte_pipeline_table_params table_params = {
377                         .ops = &rte_table_hash_key32_lru_ops,
378                         .arg_create = &table_hash_params,
379                         .f_action_hit = NULL,
380                         .f_action_miss = NULL,
381                         .arg_ah = NULL,
382                         .action_data_size = 0,
383                 };
384
385                 if (rte_pipeline_table_create(p, &table_params, &table_id))
386                         rte_panic("Unable to configure the hash table\n");
387         }
388         break;
389
390         case e_APP_PIPELINE_HASH_CUCKOO_KEY8:
391         case e_APP_PIPELINE_HASH_CUCKOO_KEY16:
392         case e_APP_PIPELINE_HASH_CUCKOO_KEY32:
393         case e_APP_PIPELINE_HASH_CUCKOO_KEY48:
394         case e_APP_PIPELINE_HASH_CUCKOO_KEY64:
395         case e_APP_PIPELINE_HASH_CUCKOO_KEY80:
396         case e_APP_PIPELINE_HASH_CUCKOO_KEY96:
397         case e_APP_PIPELINE_HASH_CUCKOO_KEY112:
398         case e_APP_PIPELINE_HASH_CUCKOO_KEY128:
399         {
400                 char hash_name[RTE_HASH_NAMESIZE];
401
402                 snprintf(hash_name, sizeof(hash_name), "RTE_TH_CUCKOO_%d",
403                         app.pipeline_type);
404
405                 struct rte_table_hash_cuckoo_params table_hash_params = {
406                         .key_size = key_size,
407                         .n_keys = (1 << 24) + 1,
408                         .f_hash = test_hash,
409                         .seed = 0,
410                         .signature_offset = APP_METADATA_OFFSET(0),
411                         .key_offset = APP_METADATA_OFFSET(32),
412                         .name = hash_name,
413                 };
414
415                 struct rte_pipeline_table_params table_params = {
416                         .ops = &rte_table_hash_cuckoo_ops,
417                         .arg_create = &table_hash_params,
418                         .f_action_hit = NULL,
419                         .f_action_miss = NULL,
420                         .arg_ah = NULL,
421                         .action_data_size = 0,
422                 };
423
424                 if (rte_pipeline_table_create(p, &table_params, &table_id))
425                         rte_panic("Unable to configure the hash table\n");
426         }
427         break;
428
429         default:
430                 rte_panic("Invalid hash table type or key size\n");
431         }
432
433         /* Interconnecting ports and tables */
434         for (i = 0; i < app.n_ports; i++)
435                 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
436                         table_id))
437                         rte_panic("Unable to connect input port %u to "
438                                 "table %u\n", port_in_id[i],  table_id);
439
440         /* Add entries to tables */
441         for (i = 0; i < (1 << 24); i++) {
442                 struct rte_pipeline_table_entry entry = {
443                         .action = RTE_PIPELINE_ACTION_PORT,
444                         {.port_id = port_out_id[i & (app.n_ports - 1)]},
445                 };
446                 struct rte_pipeline_table_entry *entry_ptr;
447                 uint8_t key[32];
448                 uint32_t *k32 = (uint32_t *) key;
449                 int key_found, status;
450
451                 memset(key, 0, sizeof(key));
452                 k32[0] = rte_be_to_cpu_32(i);
453
454                 status = rte_pipeline_table_entry_add(p, table_id, key, &entry,
455                         &key_found, &entry_ptr);
456                 if (status < 0)
457                         rte_panic("Unable to add entry to table %u (%d)\n",
458                                 table_id, status);
459         }
460
461         /* Enable input ports */
462         for (i = 0; i < app.n_ports; i++)
463                 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
464                         rte_panic("Unable to enable input port %u\n",
465                                 port_in_id[i]);
466
467         /* Check pipeline consistency */
468         if (rte_pipeline_check(p) < 0)
469                 rte_panic("Pipeline consistency check failed\n");
470
471         /* Run-time */
472 #if APP_FLUSH == 0
473         for ( ; ; )
474                 rte_pipeline_run(p);
475 #else
476         for (i = 0; ; i++) {
477                 rte_pipeline_run(p);
478
479                 if ((i & APP_FLUSH) == 0)
480                         rte_pipeline_flush(p);
481         }
482 #endif
483 }
484
485 uint64_t test_hash(
486         void *key,
487         __attribute__((unused)) uint32_t key_size,
488         __attribute__((unused)) uint64_t seed)
489 {
490         uint32_t *k32 = key;
491         uint32_t ip_dst = rte_be_to_cpu_32(k32[0]);
492         uint64_t signature = (ip_dst >> 2) | ((ip_dst & 0x3) << 30);
493
494         return signature;
495 }
496
497 void
498 app_main_loop_rx_metadata(void) {
499         uint32_t i, j;
500         int ret;
501
502         RTE_LOG(INFO, USER1, "Core %u is doing RX (with meta-data)\n",
503                 rte_lcore_id());
504
505         for (i = 0; ; i = ((i + 1) & (app.n_ports - 1))) {
506                 uint16_t n_mbufs;
507
508                 n_mbufs = rte_eth_rx_burst(
509                         app.ports[i],
510                         0,
511                         app.mbuf_rx.array,
512                         app.burst_size_rx_read);
513
514                 if (n_mbufs == 0)
515                         continue;
516
517                 for (j = 0; j < n_mbufs; j++) {
518                         struct rte_mbuf *m;
519                         uint8_t *m_data, *key;
520                         struct ipv4_hdr *ip_hdr;
521                         struct ipv6_hdr *ipv6_hdr;
522                         uint32_t ip_dst;
523                         uint8_t *ipv6_dst;
524                         uint32_t *signature, *k32;
525
526                         m = app.mbuf_rx.array[j];
527                         m_data = rte_pktmbuf_mtod(m, uint8_t *);
528                         signature = RTE_MBUF_METADATA_UINT32_PTR(m,
529                                         APP_METADATA_OFFSET(0));
530                         key = RTE_MBUF_METADATA_UINT8_PTR(m,
531                                         APP_METADATA_OFFSET(32));
532
533                         if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
534                                 ip_hdr = (struct ipv4_hdr *)
535                                         &m_data[sizeof(struct ether_hdr)];
536                                 ip_dst = ip_hdr->dst_addr;
537
538                                 k32 = (uint32_t *) key;
539                                 k32[0] = ip_dst & 0xFFFFFF00;
540                         } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
541                                 ipv6_hdr = (struct ipv6_hdr *)
542                                         &m_data[sizeof(struct ether_hdr)];
543                                 ipv6_dst = ipv6_hdr->dst_addr;
544
545                                 memcpy(key, ipv6_dst, 16);
546                         } else
547                                 continue;
548
549                         *signature = test_hash(key, 0, 0);
550                 }
551
552                 do {
553                         ret = rte_ring_sp_enqueue_bulk(
554                                 app.rings_rx[i],
555                                 (void **) app.mbuf_rx.array,
556                                 n_mbufs,
557                                 NULL);
558                 } while (ret == 0);
559         }
560 }