Skip to content

Commit

Permalink
routing: add netlink protocol type for BGP routes
Browse files Browse the repository at this point in the history
While here, add a helper function to avoid code duplication.

Signed-off-by: Renato Westphal <[email protected]>
  • Loading branch information
rwestphal committed Feb 27, 2024
1 parent 2980c6b commit 0a92a02
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions holo-routing/src/netlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@ use tracing::error;

use crate::rib::Route;

// Route protocol types as defined in the rtnetlink.h kernel header.
const NETLINK_PROTO_UNSPEC: u8 = 0;
const NETLINK_PROTO_BGP: u8 = 186;
const NETLINK_PROTO_OSPF: u8 = 188;
const NETLINK_PROTO_RIP: u8 = 189;

fn netlink_protocol(protocol: Protocol) -> u8 {
match protocol {
Protocol::BGP => NETLINK_PROTO_BGP,
Protocol::OSPFV2 | Protocol::OSPFV3 => NETLINK_PROTO_OSPF,
Protocol::RIPV2 | Protocol::RIPNG => NETLINK_PROTO_RIP,
_ => NETLINK_PROTO_UNSPEC,
}
}

pub(crate) async fn ip_route_install(
handle: &Handle,
prefix: &IpNetwork,
Expand All @@ -29,11 +40,7 @@ pub(crate) async fn ip_route_install(
let mut request = handle.route().add();

// Set route protocol.
let protocol = match route.protocol {
Protocol::OSPFV2 | Protocol::OSPFV3 => NETLINK_PROTO_OSPF,
Protocol::RIPV2 | Protocol::RIPNG => NETLINK_PROTO_RIP,
_ => NETLINK_PROTO_UNSPEC,
};
let protocol = netlink_protocol(route.protocol);
request = request.protocol(protocol);

match prefix {
Expand Down Expand Up @@ -113,11 +120,7 @@ pub(crate) async fn ip_route_uninstall(
let mut request = handle.route().add();

// Set route protocol.
let protocol = match protocol {
Protocol::OSPFV2 | Protocol::OSPFV3 => NETLINK_PROTO_OSPF,
Protocol::RIPV2 | Protocol::RIPNG => NETLINK_PROTO_RIP,
_ => NETLINK_PROTO_UNSPEC,
};
let protocol = netlink_protocol(protocol);
request = request.protocol(protocol);

match prefix {
Expand Down

0 comments on commit 0a92a02

Please sign in to comment.