examples/pipeline: support hash functions
[dpdk.git] / examples / pipeline / examples / hash_func.spec
1 ; SPDX-License-Identifier: BSD-3-Clause
2 ; Copyright(c) 2022 Intel Corporation
3
4 ; This simple example illustrates how to compute a hash signature over an n-tuple set of fields read
5 ; from the packet headers and/or the packet meta-data by using the "hash" instruction. In this
6 ; specific example, the n-tuple is the classical DiffServ 5-tuple.
7
8 //
9 // Headers
10 //
11 struct ethernet_h {
12         bit<48> dst_addr
13         bit<48> src_addr
14         bit<16> ethertype
15 }
16
17 struct ipv4_h {
18         bit<8> ver_ihl
19         bit<8> diffserv
20         bit<16> total_len
21         bit<16> identification
22         bit<16> flags_offset
23         bit<8> ttl
24         bit<8> protocol
25         bit<16> hdr_checksum
26         bit<32> src_addr
27         bit<32> dst_addr
28 }
29
30 struct udp_h {
31         bit<16> src_port
32         bit<16> dst_port
33         bit<16> length
34         bit<16> checksum
35 }
36
37 header ethernet instanceof ethernet_h
38 header ipv4 instanceof ipv4_h
39 header udp instanceof udp_h
40
41 //
42 // Meta-data.
43 //
44 struct metadata_t {
45         bit<32> port
46         bit<32> src_addr
47         bit<32> dst_addr
48         bit<8> protocol
49         bit<16> src_port
50         bit<16> dst_port
51         bit<32> hash
52 }
53
54 metadata instanceof metadata_t
55
56 //
57 // Pipeline.
58 //
59 apply {
60         //
61         // RX and parse.
62         //
63         rx m.port
64         extract h.ethernet
65         extract h.ipv4
66         extract h.udp
67
68         //
69         // Prepare the n-tuple to be hashed in meta-data.
70         //
71         // This is required when:
72         //    a) The n-tuple fields are part of different headers;
73         //    b) Some n-tuple fields come from headers and some from meta-data.
74         //
75         mov m.src_addr h.ipv4.src_addr
76         mov m.dst_addr h.ipv4.dst_addr
77         mov m.protocol h.ipv4.protocol
78         mov m.src_port h.udp.src_port
79         mov m.dst_port h.udp.dst_port
80
81         //
82         // Compute the hash over the n-tuple.
83         //
84         // Details:
85         //    a) Hash function: jhash. Another available option is crc32.
86         //    b) Destination (i.e. hash result): m.hash;
87         //    c) Source (i.e. n-tuple to be hashed): The 5-tuple formed by the meta-data fields
88         //       (m.src_addr, m.dst_addr, m.protocol, m.src_port, m.dst_port). Only the first and
89         //       the last n-tuple fields are specified in the hash instruction, but all the fields
90         //       in between are part of the n-tuple to be hashed.
91         //
92         hash jhash m.hash m.src_addr m.dst_port
93
94         //
95         // Use the computed hash to create a uniform distribution of pkts across the 4 output ports.
96         //
97         and m.hash 3
98         mov m.port m.hash
99
100         //
101         // De-parse and TX.
102         //
103         emit h.ethernet
104         emit h.ipv4
105         emit h.udp
106         tx m.port
107 }