Skip to content

Commit

Permalink
netlink init.
Browse files Browse the repository at this point in the history
Signed-off-by: Klaus Ma <klausm@nvidia.com>
  • Loading branch information
k82cn committed Oct 29, 2024
1 parent c3e3ae8 commit 98602bd
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 46 deletions.
102 changes: 56 additions & 46 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ nix = { version = "0.29", features = [
"mount",
"user",
"process",
"socket",
] }
oci-spec = "0.7.0"
flate2 = "1.0"
Expand Down
2 changes: 2 additions & 0 deletions src/apis/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub enum ChariotError {
IO(String),
#[error("{0}")]
Json(String),
#[error("{0}")]
Internal(String),
}

impl From<io::Error> for ChariotError {
Expand Down
2 changes: 2 additions & 0 deletions src/core/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ limitations under the License.
mod cfg;
mod cgroup;
mod cmd;
mod netlink;
mod network;

use std::error::Error;

Expand Down
57 changes: 57 additions & 0 deletions src/core/netlink/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2024 The openBCE Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

use std::os::fd::AsRawFd;

use chariot::apis::{ChariotError, ChariotResult};

use nix::sys::socket::{
bind, socket, AddressFamily, NetlinkAddr, SockFlag, SockProtocol, SockType,
};
use nix::unistd::getpid;

pub fn add_route() -> ChariotResult<()> {
let fd = socket(
AddressFamily::Netlink,
SockType::Raw,
SockFlag::empty(),
SockProtocol::NetlinkRoute,
)?;

let pid = {
let pid = getpid().as_raw();
if pid < 0 {
Err(Box::new(ChariotError::Internal("invalid pid".to_string())))
} else {
Ok(pid)
}
}?;

let addr = NetlinkAddr::new(pid as u32, 0);

bind(fd.as_raw_fd(), &addr)?;

Ok(())
}

pub fn delete_route() -> ChariotResult<()> {
todo!()
}

pub fn add_snat() -> ChariotResult<()> {
todo!()
}

pub fn add_dnat() -> ChariotResult<()> {
todo!()
}
15 changes: 15 additions & 0 deletions src/core/network/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Copyright 2024 The openBCE Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

mod bridge;
mod ovs;
12 changes: 12 additions & 0 deletions src/core/network/ovs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Copyright 2024 The openBCE Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

0 comments on commit 98602bd

Please sign in to comment.