Equal-Cost Multi-Path Routing Example
Equal-Cost Multi-Path Routing (ECMP) is a network routing strategy that enables packet forwarding to a single destination over multiple paths that have the same routing cost or metric. This approach is widely used in modern data center and enterprise networks to improve bandwidth utilization, redundancy, and network resilience. ECMP allows the eSwitch on the DPU install multiple next-hop entries for a destination prefix if those paths have equal cost according a hash function based on selected packet header fields. When forwarding packets, the DPL program can select among these equal-cost paths, ensuring that all packets belonging to the same flow follow the same path, while different flows may be distributed across different paths.
The ECMP metadata is a struct that will be used to store the result of a hash calculation over the desired fields.
struct
usermeta_t {
bit<4> ecmp_select;
};
In he main control of the ECMP example, the logic is as follows:
create a NvHash object
define an ECMP group table that filters out the destination IP addresses that will be routed
calculate the hash over the desired packet fields
apply a second table that matches on the hash result, and performs the next hop according to the table entries.
#define ECMP_BUCKETS 16
control c(
inout nv_headers_t headers,
in nv_standard_metadata_t std_meta,
inout usermeta_t user_meta,
inout nv_empty_metadata_t pkt_out_meta
) {
// Create a hash object with the selected algorithm
NvHash(NvHashAlgorithm.CRC32) hash;
action drop() {
nv_drop();
}
action next_hop(
nv_mac_addr_t dmac,
nv_ipv4_addr_t ipv4,
nv_logical_port_t port
) {
headers.ethernet.dst_addr = dmac;
headers.ipv4.dst_addr = ipv4;
headers.ipv4.ttl -= 1;
nv_send_to_port(port);
}
table ecmp_group {
key = {
headers.ipv4.dst_addr: lpm;
}
actions = {
drop;
NoAction;
}
default_action = drop;
size = 1024;
}
table ecmp_nhop {
key = {
user_meta.ecmp_select: exact;
}
actions = {
drop;
next_hop;
}
default_action = drop;
size = ECMP_BUCKETS;
}
In this apply block example, once the packet is validated and part of an ECMP group policy, the hash is calculated over the 5-tuple. Then the next hop is applied with a final destination.
The user has control of how many ECMP buckets are needed. By varying the entries added to the ecmp_nhop table, equal cost can be assigned to each final port destination. Fine grain weighted ECMP can be achieved by increasing the number of buckets and by weighting the entries in an unequal distribution (e.g. to favor one path more than the others). The DPL programmer can also create additional custom logic to affect the distribution.
apply {
if
(headers.ipv4.isValid() && std_meta.is_l4_ok && headers.ipv4.ttl != 0) {
ecmp_group.apply();
user_meta.ecmp_select =
(bit<4>)hash.get_hash(
ECMP_BUCKETS, // size
{
headers.ipv4.src_addr,
headers.ipv4.dst_addr,
headers.ipv4.protocol,
std_meta.l4_src_port,
std_meta.l4_dst_port
}
);
ecmp_nhop.apply();
}
}
For more information, refer to the full DPL example, ecmp.p4.