New upstream version 17.11-rc3
[deb_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         struct rte_table_hash_params table_hash_params = {
173                 .name = "TABLE",
174                 .key_size = key_size,
175                 .key_offset = APP_METADATA_OFFSET(32),
176                 .key_mask = NULL,
177                 .n_keys = 1 << 24,
178                 .n_buckets = 1 << 22,
179                 .f_hash = test_hash,
180                 .seed = 0,
181         };
182
183         /* Table configuration */
184         switch (app.pipeline_type) {
185         case e_APP_PIPELINE_HASH_KEY8_EXT:
186         case e_APP_PIPELINE_HASH_KEY16_EXT:
187         case e_APP_PIPELINE_HASH_KEY32_EXT:
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_pipeline_table_params table_params = {
208                         .ops = &rte_table_hash_lru_ops,
209                         .arg_create = &table_hash_params,
210                         .f_action_hit = NULL,
211                         .f_action_miss = NULL,
212                         .arg_ah = NULL,
213                         .action_data_size = 0,
214                 };
215
216                 if (rte_pipeline_table_create(p, &table_params, &table_id))
217                         rte_panic("Unable to configure the hash table\n");
218         }
219         break;
220
221         case e_APP_PIPELINE_HASH_SPEC_KEY8_EXT:
222         {
223                 struct rte_pipeline_table_params table_params = {
224                         .ops = &rte_table_hash_key8_ext_ops,
225                         .arg_create = &table_hash_params,
226                         .f_action_hit = NULL,
227                         .f_action_miss = NULL,
228                         .arg_ah = NULL,
229                         .action_data_size = 0,
230                 };
231
232                 if (rte_pipeline_table_create(p, &table_params, &table_id))
233                         rte_panic("Unable to configure the hash table\n");
234         }
235         break;
236
237         case e_APP_PIPELINE_HASH_SPEC_KEY8_LRU:
238         {
239                 struct rte_pipeline_table_params table_params = {
240                         .ops = &rte_table_hash_key8_lru_ops,
241                         .arg_create = &table_hash_params,
242                         .f_action_hit = NULL,
243                         .f_action_miss = NULL,
244                         .arg_ah = NULL,
245                         .action_data_size = 0,
246                 };
247
248                 if (rte_pipeline_table_create(p, &table_params, &table_id))
249                         rte_panic("Unable to configure the hash table\n");
250         }
251         break;
252
253         case e_APP_PIPELINE_HASH_SPEC_KEY16_EXT:
254         {
255                 struct rte_pipeline_table_params table_params = {
256                         .ops = &rte_table_hash_key16_ext_ops,
257                         .arg_create = &table_hash_params,
258                         .f_action_hit = NULL,
259                         .f_action_miss = NULL,
260                         .arg_ah = NULL,
261                         .action_data_size = 0,
262                 };
263
264                 if (rte_pipeline_table_create(p, &table_params, &table_id))
265                         rte_panic("Unable to configure the hash table)\n");
266         }
267         break;
268
269         case e_APP_PIPELINE_HASH_SPEC_KEY16_LRU:
270         {
271                 struct rte_pipeline_table_params table_params = {
272                         .ops = &rte_table_hash_key16_lru_ops,
273                         .arg_create = &table_hash_params,
274                         .f_action_hit = NULL,
275                         .f_action_miss = NULL,
276                         .arg_ah = NULL,
277                         .action_data_size = 0,
278                 };
279
280                 if (rte_pipeline_table_create(p, &table_params, &table_id))
281                         rte_panic("Unable to configure the hash table\n");
282         }
283         break;
284
285         case e_APP_PIPELINE_HASH_SPEC_KEY32_EXT:
286         {
287                 struct rte_pipeline_table_params table_params = {
288                         .ops = &rte_table_hash_key32_ext_ops,
289                         .arg_create = &table_hash_params,
290                         .f_action_hit = NULL,
291                         .f_action_miss = NULL,
292                         .arg_ah = NULL,
293                         .action_data_size = 0,
294                 };
295
296                 if (rte_pipeline_table_create(p, &table_params, &table_id))
297                         rte_panic("Unable to configure the hash table\n");
298         }
299         break;
300
301
302         case e_APP_PIPELINE_HASH_SPEC_KEY32_LRU:
303         {
304                 struct rte_pipeline_table_params table_params = {
305                         .ops = &rte_table_hash_key32_lru_ops,
306                         .arg_create = &table_hash_params,
307                         .f_action_hit = NULL,
308                         .f_action_miss = NULL,
309                         .arg_ah = NULL,
310                         .action_data_size = 0,
311                 };
312
313                 if (rte_pipeline_table_create(p, &table_params, &table_id))
314                         rte_panic("Unable to configure the hash table\n");
315         }
316         break;
317
318         case e_APP_PIPELINE_HASH_CUCKOO_KEY8:
319         case e_APP_PIPELINE_HASH_CUCKOO_KEY16:
320         case e_APP_PIPELINE_HASH_CUCKOO_KEY32:
321         case e_APP_PIPELINE_HASH_CUCKOO_KEY48:
322         case e_APP_PIPELINE_HASH_CUCKOO_KEY64:
323         case e_APP_PIPELINE_HASH_CUCKOO_KEY80:
324         case e_APP_PIPELINE_HASH_CUCKOO_KEY96:
325         case e_APP_PIPELINE_HASH_CUCKOO_KEY112:
326         case e_APP_PIPELINE_HASH_CUCKOO_KEY128:
327         {
328                 struct rte_pipeline_table_params table_params = {
329                         .ops = &rte_table_hash_cuckoo_ops,
330                         .arg_create = &table_hash_params,
331                         .f_action_hit = NULL,
332                         .f_action_miss = NULL,
333                         .arg_ah = NULL,
334                         .action_data_size = 0,
335                 };
336
337                 if (rte_pipeline_table_create(p, &table_params, &table_id))
338                         rte_panic("Unable to configure the hash table\n");
339         }
340         break;
341
342         default:
343                 rte_panic("Invalid hash table type or key size\n");
344         }
345
346         /* Interconnecting ports and tables */
347         for (i = 0; i < app.n_ports; i++)
348                 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
349                         table_id))
350                         rte_panic("Unable to connect input port %u to "
351                                 "table %u\n", port_in_id[i],  table_id);
352
353         /* Add entries to tables */
354         for (i = 0; i < (1 << 24); i++) {
355                 struct rte_pipeline_table_entry entry = {
356                         .action = RTE_PIPELINE_ACTION_PORT,
357                         {.port_id = port_out_id[i & (app.n_ports - 1)]},
358                 };
359                 struct rte_pipeline_table_entry *entry_ptr;
360                 uint8_t key[32];
361                 uint32_t *k32 = (uint32_t *) key;
362                 int key_found, status;
363
364                 memset(key, 0, sizeof(key));
365                 k32[0] = rte_be_to_cpu_32(i);
366
367                 status = rte_pipeline_table_entry_add(p, table_id, key, &entry,
368                         &key_found, &entry_ptr);
369                 if (status < 0)
370                         rte_panic("Unable to add entry to table %u (%d)\n",
371                                 table_id, status);
372         }
373
374         /* Enable input ports */
375         for (i = 0; i < app.n_ports; i++)
376                 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
377                         rte_panic("Unable to enable input port %u\n",
378                                 port_in_id[i]);
379
380         /* Check pipeline consistency */
381         if (rte_pipeline_check(p) < 0)
382                 rte_panic("Pipeline consistency check failed\n");
383
384         /* Run-time */
385 #if APP_FLUSH == 0
386         for ( ; ; )
387                 rte_pipeline_run(p);
388 #else
389         for (i = 0; ; i++) {
390                 rte_pipeline_run(p);
391
392                 if ((i & APP_FLUSH) == 0)
393                         rte_pipeline_flush(p);
394         }
395 #endif
396 }
397
398 uint64_t test_hash(
399         void *key,
400         __attribute__((unused)) void *key_mask,
401         __attribute__((unused)) uint32_t key_size,
402         __attribute__((unused)) uint64_t seed)
403 {
404         uint32_t *k32 = key;
405         uint32_t ip_dst = rte_be_to_cpu_32(k32[0]);
406         uint64_t signature = (ip_dst >> 2) | ((ip_dst & 0x3) << 30);
407
408         return signature;
409 }
410
411 void
412 app_main_loop_rx_metadata(void) {
413         uint32_t i, j;
414         int ret;
415
416         RTE_LOG(INFO, USER1, "Core %u is doing RX (with meta-data)\n",
417                 rte_lcore_id());
418
419         for (i = 0; ; i = ((i + 1) & (app.n_ports - 1))) {
420                 uint16_t n_mbufs;
421
422                 n_mbufs = rte_eth_rx_burst(
423                         app.ports[i],
424                         0,
425                         app.mbuf_rx.array,
426                         app.burst_size_rx_read);
427
428                 if (n_mbufs == 0)
429                         continue;
430
431                 for (j = 0; j < n_mbufs; j++) {
432                         struct rte_mbuf *m;
433                         uint8_t *m_data, *key;
434                         struct ipv4_hdr *ip_hdr;
435                         struct ipv6_hdr *ipv6_hdr;
436                         uint32_t ip_dst;
437                         uint8_t *ipv6_dst;
438                         uint32_t *signature, *k32;
439
440                         m = app.mbuf_rx.array[j];
441                         m_data = rte_pktmbuf_mtod(m, uint8_t *);
442                         signature = RTE_MBUF_METADATA_UINT32_PTR(m,
443                                         APP_METADATA_OFFSET(0));
444                         key = RTE_MBUF_METADATA_UINT8_PTR(m,
445                                         APP_METADATA_OFFSET(32));
446
447                         if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
448                                 ip_hdr = (struct ipv4_hdr *)
449                                         &m_data[sizeof(struct ether_hdr)];
450                                 ip_dst = ip_hdr->dst_addr;
451
452                                 k32 = (uint32_t *) key;
453                                 k32[0] = ip_dst & 0xFFFFFF00;
454                         } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
455                                 ipv6_hdr = (struct ipv6_hdr *)
456                                         &m_data[sizeof(struct ether_hdr)];
457                                 ipv6_dst = ipv6_hdr->dst_addr;
458
459                                 memcpy(key, ipv6_dst, 16);
460                         } else
461                                 continue;
462
463                         *signature = test_hash(key, NULL, 0, 0);
464                 }
465
466                 do {
467                         ret = rte_ring_sp_enqueue_bulk(
468                                 app.rings_rx[i],
469                                 (void **) app.mbuf_rx.array,
470                                 n_mbufs,
471                                 NULL);
472                 } while (ret == 0);
473         }
474 }