Skip to content

Commit

Permalink
feat(rsln): implement list links
Browse files Browse the repository at this point in the history
  • Loading branch information
wqld committed Mar 17, 2024
1 parent 92f3b37 commit 22c110f
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

Sinabro is a networking solution for Kubernetes that leverages eBPF to provide high-performance networking and security features.

## Components

- **[agent](https://github.com/wqld/sinabro/tree/main/agent)**: The Sinabro agent is a daemon that runs on each node in the Kubernetes cluster. It is responsible for managing the network interfaces and routing tables required by the Sinabro CNI. For high performance, it utilizes eBPF programs.
- **[cni](https://github.com/wqld/sinabro/tree/main/cni)**: The Sinabro CNI is a container network interface plugin that is responsible for setting up the network interfaces and routing tables required by the pods in the Kubernetes cluster.
- **[rsln](https://github.com/wqld/sinabro/tree/main/rsln)**: A netlink library implemented in Rust that provides the netlink protocol based kernel interfaces required by the Sinabro project.
- **[wgctrl](https://github.com/wqld/sinabro/tree/main/wgctrl)**: A Rust implementation of the [wgctrl-go](https://github.com/WireGuard/wgctrl-go) project, this is a WireGuard control library that provides the WireGuard control interface required by the Sinabro project.

## Getting Started

Sinabro is currently in its early stages of development. The ongoing development is being carried out in the following environment, which is also the verified execution environment:
Expand Down
2 changes: 1 addition & 1 deletion agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ ws = ["kube/ws"]

[dependencies]
sinabro-config = { path = "../config" }
rsln = { path = "../rsln" }

axum = "0.7.2"
aya = { git = "https://github.com/aya-rs/aya", rev = "1979da92a722bacd9c984865a4c7108e22fb618f", features = [
Expand All @@ -27,6 +26,7 @@ log = "0.4"
ipnet = "2.9.0"
kube = { version = "0.88.1", features = ["runtime", "derive"] }
k8s-openapi = { version = "0.21.0", features = ["latest"] }
rsln = "0.0"
serde = "1.0"
serde_yaml = "0.9"
serde_json = "1.0"
Expand Down
5 changes: 1 addition & 4 deletions agent/src/netlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ impl<'a> DerefMut for Netlink<'a> {

impl<'a> Netlink<'a> {
pub fn new() -> Self {
Self {
netlink: rsln::netlink::Netlink::new(),
..Default::default()
}
Self::default()
}

pub fn init(host_ip: &'a str, pod_cidr: &'a IpNet, node_routes: &'a [NodeRoute]) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion cni/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2021"

[dependencies]
sinabro-config = { path = "../config" }
rsln = { path = "../rsln" }

anyhow = "1.0"
async-trait = "0.1"
Expand All @@ -16,6 +15,7 @@ once_cell = "1.19"
openssl = { version = "0.10", features = ["vendored"] }
rand = "0.8.5"
reqwest = { version = "0.11", features = ["json"] }
rsln = "0.0"
serde = "1.0"
serde_json = "1.0"
tokio = { version = "1", features = ["full"] }
Expand Down
1 change: 1 addition & 0 deletions rsln/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ tokio = { version = "1", features = ["full"] }
nix = { version = "0.28.0", features = ["sched", "user"] }
derive_builder = "0.20.0"
sysctl = "0.5"
rayon = "1.9"
27 changes: 27 additions & 0 deletions rsln/src/handle/link.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::ops::{Deref, DerefMut};

use anyhow::{anyhow, Result};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

use crate::{
core::message::Message,
Expand Down Expand Up @@ -137,6 +138,20 @@ impl LinkHandle<'_> {
}
}

pub fn list(&mut self) -> Result<Vec<Box<dyn Link>>> {
let mut req = Message::new(libc::RTM_GETLINK, libc::NLM_F_DUMP);
let msg = LinkMessage::new(libc::AF_UNSPEC);
let attr = RouteAttr::new(libc::IFLA_EXT_MASK, &libc::RTEXT_FILTER_VF.to_ne_bytes());
req.add(&msg.serialize()?);
req.add(&attr.serialize()?);

let res = self.request(&mut req, libc::RTM_NEWLINK)?;

res.par_iter()
.map(|m| Ok(Kind::from(m.as_slice()).into_boxed()))
.collect()
}

pub fn up<T: Link + ?Sized>(&mut self, link: &T) -> Result<()> {
let mut req = Message::new(libc::RTM_NEWLINK, libc::NLM_F_ACK);
let base = link.attrs();
Expand Down Expand Up @@ -364,4 +379,16 @@ mod tests {
assert_eq!(link.attrs().index, 1);
assert_eq!(link.attrs().name, "lo");
}

#[test]
fn test_link_list() {
test_setup!();
let mut handle = sock_handle::SocketHandle::new(libc::NETLINK_ROUTE);
let mut link_handle = handle.handle_link();

let links = link_handle.list().unwrap();

assert!(!links.is_empty());
assert!(links.iter().any(|link| link.attrs().name == "lo"));
}
}
18 changes: 18 additions & 0 deletions rsln/src/netlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ impl Netlink {
Ok(())
}

pub fn link_list(&mut self) -> Result<Vec<Box<dyn Link>>> {
self.sockets
.entry(libc::NETLINK_ROUTE)
.or_insert(SocketHandle::new(libc::NETLINK_ROUTE))
.handle_link()
.list()
}

pub fn link_get(&mut self, attr: &LinkAttrs) -> Result<Box<dyn Link>> {
self.sockets
.entry(libc::NETLINK_ROUTE)
Expand Down Expand Up @@ -269,4 +277,14 @@ mod tests {
assert!(link.is_ok());
println!("{:?}", link.unwrap().kind());
}

#[test]
fn test_list_links() {
test_setup!();
let mut netlink = Netlink::new();
let links = netlink.link_list().unwrap();

assert!(!links.is_empty());
assert!(links.iter().any(|link| link.attrs().name == "lo"));
}
}
6 changes: 6 additions & 0 deletions rsln/src/types/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ impl From<&[u8]> for Kind {
}
}

impl Kind {
pub fn into_boxed(self) -> Box<dyn Link> {
Box::new(self)
}
}

pub trait Link: Send {
fn link_type(&self) -> &str;
fn attrs(&self) -> &LinkAttrs;
Expand Down

0 comments on commit 22c110f

Please sign in to comment.