Skip to content

Commit 3b28b99

Browse files
committed
route: add support for MPLS routes and nexthop label stacks
Extend `RouteMessageBuilder` to support the creation of MPLS routes and nexthops with MPLS label stacks. This enables configuration of IP-to-MPLS, MPLS-to-MPLS, and MPLS-to-IP routes. Signed-off-by: Renato Westphal <[email protected]>
1 parent 12449ee commit 3b28b99

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/route/builder.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use std::{
77

88
use netlink_packet_route::{
99
route::{
10-
RouteAddress, RouteAttribute, RouteFlags, RouteHeader, RouteMessage,
10+
MplsLabel, RouteAddress, RouteAttribute, RouteFlags, RouteHeader,
11+
RouteLwEnCapType, RouteLwTunnelEncap, RouteMessage, RouteMplsIpTunnel,
1112
RouteProtocol, RouteScope, RouteType, RouteVia,
1213
},
1314
AddressFamily,
@@ -40,6 +41,16 @@ impl<T> RouteMessageBuilder<T> {
4041
}
4142
}
4243

44+
/// Sets the destination MPLS label.
45+
pub fn destination_mpls(mut self, label: MplsLabel) -> Self {
46+
self.message.header.address_family = AddressFamily::Mpls;
47+
self.message.header.destination_prefix_length = 20;
48+
self.message
49+
.attributes
50+
.push(RouteAttribute::Destination(RouteAddress::Mpls(label)));
51+
self
52+
}
53+
4354
/// Sets the input interface index.
4455
pub fn input_interface(mut self, index: u32) -> Self {
4556
self.message.attributes.push(RouteAttribute::Iif(index));
@@ -52,6 +63,29 @@ impl<T> RouteMessageBuilder<T> {
5263
self
5364
}
5465

66+
/// Sets the output MPLS encapsulation labels.
67+
pub fn output_mpls(mut self, labels: Vec<MplsLabel>) -> Self {
68+
if labels.is_empty() {
69+
return self;
70+
}
71+
if self.message.header.address_family == AddressFamily::Mpls {
72+
self.message
73+
.attributes
74+
.push(RouteAttribute::NewDestination(labels));
75+
} else {
76+
self.message
77+
.attributes
78+
.push(RouteAttribute::EncapType(RouteLwEnCapType::Mpls));
79+
let encap = RouteLwTunnelEncap::Mpls(
80+
RouteMplsIpTunnel::Destination(labels),
81+
);
82+
self.message
83+
.attributes
84+
.push(RouteAttribute::Encap(vec![encap]));
85+
}
86+
self
87+
}
88+
5589
/// Sets the route priority (metric)
5690
pub fn priority(mut self, priority: u32) -> Self {
5791
self.message
@@ -374,6 +408,7 @@ impl RouteMessageBuilder<IpAddr> {
374408
RouteAttribute::Gateway(addr.into())
375409
}
376410
(Inet, IpAddr::V6(v6)) => RouteAttribute::Via(RouteVia::Inet6(v6)),
411+
(Mpls, _) => RouteAttribute::Via(addr.into()),
377412
(af, _) => return Err(InvalidRouteMessage::AddressFamily(af)),
378413
};
379414
self.message.attributes.push(attr);

0 commit comments

Comments
 (0)