Skip to content

Commit aacfc04

Browse files
committed
Improve ECMP hash guidance in load balancing tutorial
Signed-off-by: AyushJoshi123 <joshiayush00712345@gmail.com>
1 parent df4a900 commit aacfc04

1 file changed

Lines changed: 41 additions & 6 deletions

File tree

exercises/load_balance/load_balance.p4

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,42 @@ control MyIngress(inout headers hdr,
104104
mark_to_drop(standard_metadata);
105105
}
106106
action set_ecmp_select(bit<16> ecmp_base, bit<32> ecmp_count) {
107-
/* TODO: hash on 5-tuple and save the hash result in meta.ecmp_select
108-
so that the ecmp_nhop table can use it to make a forwarding decision accordingly */
109-
}
107+
108+
/* Compute ECMP path selection using a CRC16 hash over the packet 5-tuple.
109+
110+
5-tuple fields:
111+
- source IP
112+
- destination IP
113+
- protocol
114+
- TCP source port
115+
- TCP destination port
116+
117+
ecmp_base:
118+
Starting offset for the ECMP next-hop group.
119+
120+
ecmp_count:
121+
Number of equal-cost paths available.
122+
123+
The resulting hash is stored in meta.ecmp_select and later used
124+
by the ecmp_nhop table to select a forwarding path.
125+
*/
126+
127+
hash(
128+
meta.ecmp_select,
129+
HashAlgorithm.crc16,
130+
ecmp_base,
131+
{
132+
hdr.ipv4.srcAddr,
133+
hdr.ipv4.dstAddr,
134+
hdr.ipv4.protocol,
135+
hdr.tcp.srcPort,
136+
hdr.tcp.dstPort
137+
},
138+
ecmp_count
139+
);
140+
}
141+
142+
110143
action set_nhop(bit<48> nhop_dmac, bit<32> nhop_ipv4, bit<9> port) {
111144
hdr.ethernet.dstAddr = nhop_dmac;
112145
hdr.ipv4.dstAddr = nhop_ipv4;
@@ -134,9 +167,11 @@ control MyIngress(inout headers hdr,
134167
size = 2;
135168
}
136169
apply {
137-
/* TODO: apply ecmp_group table and ecmp_nhop table if IPv4 header is
138-
* valid and TTL hasn't reached zero
139-
*/
170+
if (hdr.ipv4.isValid() && hdr.ipv4.ttl > 0) {
171+
if (ecmp_group.apply().hit) {
172+
ecmp_nhop.apply();
173+
}
174+
}
140175
if (ecmp_group.apply().hit) {
141176
ecmp_nhop.apply();
142177
}

0 commit comments

Comments
 (0)