table: add key mask to 8 and 16-byte hash parameters
[dpdk.git] / app / test-pipeline / pipeline_hash.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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_pipeline.h>
47
48 #include "main.h"
49
50 static void
51 translate_options(uint32_t *special, uint32_t *ext, uint32_t *key_size)
52 {
53         switch (app.pipeline_type) {
54         case e_APP_PIPELINE_HASH_KEY8_EXT:
55                 *special = 0; *ext = 1; *key_size = 8; return;
56         case e_APP_PIPELINE_HASH_KEY8_LRU:
57                 *special = 0; *ext = 0; *key_size = 8; return;
58         case e_APP_PIPELINE_HASH_KEY16_EXT:
59                 *special = 0; *ext = 1; *key_size = 16; return;
60         case e_APP_PIPELINE_HASH_KEY16_LRU:
61                 *special = 0; *ext = 0; *key_size = 16; return;
62         case e_APP_PIPELINE_HASH_KEY32_EXT:
63                 *special = 0; *ext = 1; *key_size = 32; return;
64         case e_APP_PIPELINE_HASH_KEY32_LRU:
65                 *special = 0; *ext = 0; *key_size = 32; return;
66
67         case e_APP_PIPELINE_HASH_SPEC_KEY8_EXT:
68                 *special = 1; *ext = 1; *key_size = 8; return;
69         case e_APP_PIPELINE_HASH_SPEC_KEY8_LRU:
70                 *special = 1; *ext = 0; *key_size = 8; return;
71         case e_APP_PIPELINE_HASH_SPEC_KEY16_EXT:
72                 *special = 1; *ext = 1; *key_size = 16; return;
73         case e_APP_PIPELINE_HASH_SPEC_KEY16_LRU:
74                 *special = 1; *ext = 0; *key_size = 16; return;
75         case e_APP_PIPELINE_HASH_SPEC_KEY32_EXT:
76                 *special = 1; *ext = 1; *key_size = 32; return;
77         case e_APP_PIPELINE_HASH_SPEC_KEY32_LRU:
78                 *special = 1; *ext = 0; *key_size = 32; return;
79
80         default:
81                 rte_panic("Invalid hash table type or key size\n");
82         }
83 }
84 void
85 app_main_loop_worker_pipeline_hash(void) {
86         struct rte_pipeline_params pipeline_params = {
87                 .name = "pipeline",
88                 .socket_id = rte_socket_id(),
89         };
90
91         struct rte_pipeline *p;
92         uint32_t port_in_id[APP_MAX_PORTS];
93         uint32_t port_out_id[APP_MAX_PORTS];
94         uint32_t table_id;
95         uint32_t i;
96         uint32_t special, ext, key_size;
97
98         translate_options(&special, &ext, &key_size);
99
100         RTE_LOG(INFO, USER1, "Core %u is doing work "
101                 "(pipeline with hash table, %s, %s, %d-byte key)\n",
102                 rte_lcore_id(),
103                 special ? "specialized" : "non-specialized",
104                 ext ? "extendible bucket" : "LRU",
105                 key_size);
106
107         /* Pipeline configuration */
108         p = rte_pipeline_create(&pipeline_params);
109         if (p == NULL)
110                 rte_panic("Unable to configure the pipeline\n");
111
112         /* Input port configuration */
113         for (i = 0; i < app.n_ports; i++) {
114                 struct rte_port_ring_reader_params port_ring_params = {
115                         .ring = app.rings_rx[i],
116                 };
117
118                 struct rte_pipeline_port_in_params port_params = {
119                         .ops = &rte_port_ring_reader_ops,
120                         .arg_create = (void *) &port_ring_params,
121                         .f_action = NULL,
122                         .arg_ah = NULL,
123                         .burst_size = app.burst_size_worker_read,
124                 };
125
126                 if (rte_pipeline_port_in_create(p, &port_params,
127                         &port_in_id[i]))
128                         rte_panic("Unable to configure input port for "
129                                 "ring %d\n", i);
130         }
131
132         /* Output port configuration */
133         for (i = 0; i < app.n_ports; i++) {
134                 struct rte_port_ring_writer_params port_ring_params = {
135                         .ring = app.rings_tx[i],
136                         .tx_burst_sz = app.burst_size_worker_write,
137                 };
138
139                 struct rte_pipeline_port_out_params port_params = {
140                         .ops = &rte_port_ring_writer_ops,
141                         .arg_create = (void *) &port_ring_params,
142                         .f_action = NULL,
143                         .f_action_bulk = NULL,
144                         .arg_ah = NULL,
145                 };
146
147                 if (rte_pipeline_port_out_create(p, &port_params,
148                         &port_out_id[i]))
149                         rte_panic("Unable to configure output port for "
150                                 "ring %d\n", i);
151         }
152
153         /* Table configuration */
154         switch (app.pipeline_type) {
155         case e_APP_PIPELINE_HASH_KEY8_EXT:
156         case e_APP_PIPELINE_HASH_KEY16_EXT:
157         case e_APP_PIPELINE_HASH_KEY32_EXT:
158         {
159                 struct rte_table_hash_ext_params table_hash_params = {
160                         .key_size = key_size,
161                         .n_keys = 1 << 24,
162                         .n_buckets = 1 << 22,
163                         .n_buckets_ext = 1 << 21,
164                         .f_hash = test_hash,
165                         .seed = 0,
166                         .signature_offset = APP_METADATA_OFFSET(0),
167                         .key_offset = APP_METADATA_OFFSET(32),
168                 };
169
170                 struct rte_pipeline_table_params table_params = {
171                         .ops = &rte_table_hash_ext_ops,
172                         .arg_create = &table_hash_params,
173                         .f_action_hit = NULL,
174                         .f_action_miss = NULL,
175                         .arg_ah = NULL,
176                         .action_data_size = 0,
177                 };
178
179                 if (rte_pipeline_table_create(p, &table_params, &table_id))
180                         rte_panic("Unable to configure the hash table\n");
181         }
182         break;
183
184         case e_APP_PIPELINE_HASH_KEY8_LRU:
185         case e_APP_PIPELINE_HASH_KEY16_LRU:
186         case e_APP_PIPELINE_HASH_KEY32_LRU:
187         {
188                 struct rte_table_hash_lru_params table_hash_params = {
189                         .key_size = key_size,
190                         .n_keys = 1 << 24,
191                         .n_buckets = 1 << 22,
192                         .f_hash = test_hash,
193                         .seed = 0,
194                         .signature_offset = 0,
195                         .key_offset = 32,
196                 };
197
198                 struct rte_pipeline_table_params table_params = {
199                         .ops = &rte_table_hash_lru_ops,
200                         .arg_create = &table_hash_params,
201                         .f_action_hit = NULL,
202                         .f_action_miss = NULL,
203                         .arg_ah = NULL,
204                         .action_data_size = 0,
205                 };
206
207                 if (rte_pipeline_table_create(p, &table_params, &table_id))
208                         rte_panic("Unable to configure the hash table\n");
209         }
210         break;
211
212         case e_APP_PIPELINE_HASH_SPEC_KEY8_EXT:
213         {
214                 struct rte_table_hash_key8_ext_params table_hash_params = {
215                         .n_entries = 1 << 24,
216                         .n_entries_ext = 1 << 23,
217                         .signature_offset = APP_METADATA_OFFSET(0),
218                         .key_offset = APP_METADATA_OFFSET(32),
219                         .key_mask = NULL,
220                         .f_hash = test_hash,
221                         .seed = 0,
222                 };
223
224                 struct rte_pipeline_table_params table_params = {
225                         .ops = &rte_table_hash_key8_ext_ops,
226                         .arg_create = &table_hash_params,
227                         .f_action_hit = NULL,
228                         .f_action_miss = NULL,
229                         .arg_ah = NULL,
230                         .action_data_size = 0,
231                 };
232
233                 if (rte_pipeline_table_create(p, &table_params, &table_id))
234                         rte_panic("Unable to configure the hash table\n");
235         }
236         break;
237
238         case e_APP_PIPELINE_HASH_SPEC_KEY8_LRU:
239         {
240                 struct rte_table_hash_key8_lru_params table_hash_params = {
241                         .n_entries = 1 << 24,
242                         .signature_offset = APP_METADATA_OFFSET(0),
243                         .key_offset = APP_METADATA_OFFSET(32),
244                         .key_mask = NULL,
245                         .f_hash = test_hash,
246                         .seed = 0,
247                 };
248
249                 struct rte_pipeline_table_params table_params = {
250                         .ops = &rte_table_hash_key8_lru_ops,
251                         .arg_create = &table_hash_params,
252                         .f_action_hit = NULL,
253                         .f_action_miss = NULL,
254                         .arg_ah = NULL,
255                         .action_data_size = 0,
256                 };
257
258                 if (rte_pipeline_table_create(p, &table_params, &table_id))
259                         rte_panic("Unable to configure the hash table\n");
260         }
261         break;
262
263         case e_APP_PIPELINE_HASH_SPEC_KEY16_EXT:
264         {
265                 struct rte_table_hash_key16_ext_params table_hash_params = {
266                         .n_entries = 1 << 24,
267                         .n_entries_ext = 1 << 23,
268                         .signature_offset = APP_METADATA_OFFSET(0),
269                         .key_offset = APP_METADATA_OFFSET(32),
270                         .f_hash = test_hash,
271                         .seed = 0,
272                         .key_mask = NULL,
273                 };
274
275                 struct rte_pipeline_table_params table_params = {
276                         .ops = &rte_table_hash_key16_ext_ops,
277                         .arg_create = &table_hash_params,
278                         .f_action_hit = NULL,
279                         .f_action_miss = NULL,
280                         .arg_ah = NULL,
281                         .action_data_size = 0,
282                 };
283
284                 if (rte_pipeline_table_create(p, &table_params, &table_id))
285                         rte_panic("Unable to configure the hash table)\n");
286         }
287         break;
288
289         case e_APP_PIPELINE_HASH_SPEC_KEY16_LRU:
290         {
291                 struct rte_table_hash_key16_lru_params table_hash_params = {
292                         .n_entries = 1 << 24,
293                         .signature_offset = APP_METADATA_OFFSET(0),
294                         .key_offset = APP_METADATA_OFFSET(32),
295                         .f_hash = test_hash,
296                         .seed = 0,
297                         .key_mask = NULL,
298                 };
299
300                 struct rte_pipeline_table_params table_params = {
301                         .ops = &rte_table_hash_key16_lru_ops,
302                         .arg_create = &table_hash_params,
303                         .f_action_hit = NULL,
304                         .f_action_miss = NULL,
305                         .arg_ah = NULL,
306                         .action_data_size = 0,
307                 };
308
309                 if (rte_pipeline_table_create(p, &table_params, &table_id))
310                         rte_panic("Unable to configure the hash table\n");
311         }
312         break;
313
314         case e_APP_PIPELINE_HASH_SPEC_KEY32_EXT:
315         {
316                 struct rte_table_hash_key32_ext_params table_hash_params = {
317                         .n_entries = 1 << 24,
318                         .n_entries_ext = 1 << 23,
319                         .signature_offset = APP_METADATA_OFFSET(0),
320                         .key_offset = APP_METADATA_OFFSET(32),
321                         .f_hash = test_hash,
322                         .seed = 0,
323                 };
324
325                 struct rte_pipeline_table_params table_params = {
326                         .ops = &rte_table_hash_key32_ext_ops,
327                         .arg_create = &table_hash_params,
328                         .f_action_hit = NULL,
329                         .f_action_miss = NULL,
330                         .arg_ah = NULL,
331                         .action_data_size = 0,
332                 };
333
334                 if (rte_pipeline_table_create(p, &table_params, &table_id))
335                         rte_panic("Unable to configure the hash table\n");
336         }
337         break;
338
339
340         case e_APP_PIPELINE_HASH_SPEC_KEY32_LRU:
341         {
342                 struct rte_table_hash_key32_lru_params table_hash_params = {
343                         .n_entries = 1 << 24,
344                         .signature_offset = APP_METADATA_OFFSET(0),
345                         .key_offset = APP_METADATA_OFFSET(32),
346                         .f_hash = test_hash,
347                         .seed = 0,
348                 };
349
350                 struct rte_pipeline_table_params table_params = {
351                         .ops = &rte_table_hash_key32_lru_ops,
352                         .arg_create = &table_hash_params,
353                         .f_action_hit = NULL,
354                         .f_action_miss = NULL,
355                         .arg_ah = NULL,
356                         .action_data_size = 0,
357                 };
358
359                 if (rte_pipeline_table_create(p, &table_params, &table_id))
360                         rte_panic("Unable to configure the hash table\n");
361         }
362         break;
363
364         default:
365                 rte_panic("Invalid hash table type or key size\n");
366         }
367
368         /* Interconnecting ports and tables */
369         for (i = 0; i < app.n_ports; i++)
370                 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
371                         table_id))
372                         rte_panic("Unable to connect input port %u to "
373                                 "table %u\n", port_in_id[i],  table_id);
374
375         /* Add entries to tables */
376         for (i = 0; i < (1 << 24); i++) {
377                 struct rte_pipeline_table_entry entry = {
378                         .action = RTE_PIPELINE_ACTION_PORT,
379                         {.port_id = port_out_id[i & (app.n_ports - 1)]},
380                 };
381                 struct rte_pipeline_table_entry *entry_ptr;
382                 uint8_t key[32];
383                 uint32_t *k32 = (uint32_t *) key;
384                 int key_found, status;
385
386                 memset(key, 0, sizeof(key));
387                 k32[0] = rte_be_to_cpu_32(i);
388
389                 status = rte_pipeline_table_entry_add(p, table_id, key, &entry,
390                         &key_found, &entry_ptr);
391                 if (status < 0)
392                         rte_panic("Unable to add entry to table %u (%d)\n",
393                                 table_id, status);
394         }
395
396         /* Enable input ports */
397         for (i = 0; i < app.n_ports; i++)
398                 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
399                         rte_panic("Unable to enable input port %u\n",
400                                 port_in_id[i]);
401
402         /* Check pipeline consistency */
403         if (rte_pipeline_check(p) < 0)
404                 rte_panic("Pipeline consistency check failed\n");
405
406         /* Run-time */
407 #if APP_FLUSH == 0
408         for ( ; ; )
409                 rte_pipeline_run(p);
410 #else
411         for (i = 0; ; i++) {
412                 rte_pipeline_run(p);
413
414                 if ((i & APP_FLUSH) == 0)
415                         rte_pipeline_flush(p);
416         }
417 #endif
418 }
419
420 uint64_t test_hash(
421         void *key,
422         __attribute__((unused)) uint32_t key_size,
423         __attribute__((unused)) uint64_t seed)
424 {
425         uint32_t *k32 = (uint32_t *) key;
426         uint32_t ip_dst = rte_be_to_cpu_32(k32[0]);
427         uint64_t signature = (ip_dst >> 2) | ((ip_dst & 0x3) << 30);
428
429         return signature;
430 }
431
432 void
433 app_main_loop_rx_metadata(void) {
434         uint32_t i, j;
435         int ret;
436
437         RTE_LOG(INFO, USER1, "Core %u is doing RX (with meta-data)\n",
438                 rte_lcore_id());
439
440         for (i = 0; ; i = ((i + 1) & (app.n_ports - 1))) {
441                 uint16_t n_mbufs;
442
443                 n_mbufs = rte_eth_rx_burst(
444                         app.ports[i],
445                         0,
446                         app.mbuf_rx.array,
447                         app.burst_size_rx_read);
448
449                 if (n_mbufs == 0)
450                         continue;
451
452                 for (j = 0; j < n_mbufs; j++) {
453                         struct rte_mbuf *m;
454                         uint8_t *m_data, *key;
455                         struct ipv4_hdr *ip_hdr;
456                         struct ipv6_hdr *ipv6_hdr;
457                         uint32_t ip_dst;
458                         uint8_t *ipv6_dst;
459                         uint32_t *signature, *k32;
460
461                         m = app.mbuf_rx.array[j];
462                         m_data = rte_pktmbuf_mtod(m, uint8_t *);
463                         signature = RTE_MBUF_METADATA_UINT32_PTR(m,
464                                         APP_METADATA_OFFSET(0));
465                         key = RTE_MBUF_METADATA_UINT8_PTR(m,
466                                         APP_METADATA_OFFSET(32));
467
468                         if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
469                                 ip_hdr = (struct ipv4_hdr *)
470                                         &m_data[sizeof(struct ether_hdr)];
471                                 ip_dst = ip_hdr->dst_addr;
472
473                                 k32 = (uint32_t *) key;
474                                 k32[0] = ip_dst & 0xFFFFFF00;
475                         } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
476                                 ipv6_hdr = (struct ipv6_hdr *)
477                                         &m_data[sizeof(struct ether_hdr)];
478                                 ipv6_dst = ipv6_hdr->dst_addr;
479
480                                 memcpy(key, ipv6_dst, 16);
481                         } else
482                                 continue;
483
484                         *signature = test_hash(key, 0, 0);
485                 }
486
487                 do {
488                         ret = rte_ring_sp_enqueue_bulk(
489                                 app.rings_rx[i],
490                                 (void **) app.mbuf_rx.array,
491                                 n_mbufs);
492                 } while (ret < 0);
493         }
494 }