Skip to content

Commit

Permalink
macvlan modes: use upstream consts
Browse files Browse the repository at this point in the history
Fixes a small TODO in the code. Also improve the error message, including
"io error" makes no sense. Instead I added the actual mode name in the error.

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Nov 7, 2022
1 parent 4b23909 commit 0cc2556
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
8 changes: 0 additions & 8 deletions src/network/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
// default search domain
pub static PODMAN_DEFAULT_SEARCH_DOMAIN: &str = "dns.podman";

// Available macvlan modes
// TODO: remove constants from here after https://github.com/little-dude/netlink/pull/200
pub const MACVLAN_MODE_PRIVATE: u32 = 1;
pub const MACVLAN_MODE_VEPA: u32 = 2;
pub const MACVLAN_MODE_BRIDGE: u32 = 4;
pub const MACVLAN_MODE_PASSTHRU: u32 = 8;
pub const MACVLAN_MODE_SOURCE: u32 = 16;

// IPAM drivers
pub const IPAM_HOSTLOCAL: &str = "host-local";
pub const IPAM_DHCP: &str = "dhcp";
Expand Down
27 changes: 15 additions & 12 deletions src/network/core_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use crate::error::{ErrorWrap, NetavarkError, NetavarkResult};
use crate::network::{constants, internal_types, types};
use crate::wrap;
use log::debug;
use netlink_packet_route::{
MACVLAN_MODE_BRIDGE, MACVLAN_MODE_PASSTHRU, MACVLAN_MODE_PRIVATE, MACVLAN_MODE_SOURCE,
MACVLAN_MODE_VEPA,
};
use nix::sched;
use sha2::{Digest, Sha512};
use std::collections::HashMap;
Expand Down Expand Up @@ -195,20 +199,19 @@ impl CoreUtils {
Ok(result)
}

pub fn get_macvlan_mode_from_string(mode: &str) -> Result<u32, std::io::Error> {
// Replace to constant from library once this gets merged.
// TODO: use actual constants after https://github.com/little-dude/netlink/pull/200
pub fn get_macvlan_mode_from_string(mode: &str) -> NetavarkResult<u32> {
match mode {
"" | "bridge" => Ok(constants::MACVLAN_MODE_BRIDGE),
"private" => Ok(constants::MACVLAN_MODE_PRIVATE),
"vepa" => Ok(constants::MACVLAN_MODE_VEPA),
"passthru" => Ok(constants::MACVLAN_MODE_PASSTHRU),
"source" => Ok(constants::MACVLAN_MODE_SOURCE),
// default to bridge when unset
"" | "bridge" => Ok(MACVLAN_MODE_BRIDGE),
"private" => Ok(MACVLAN_MODE_PRIVATE),
"vepa" => Ok(MACVLAN_MODE_VEPA),
"passthru" => Ok(MACVLAN_MODE_PASSTHRU),
"source" => Ok(MACVLAN_MODE_SOURCE),
// default to bridge
_ => Err(std::io::Error::new(
std::io::ErrorKind::Other,
"invalid macvlan mode",
)),
name => Err(NetavarkError::msg(format!(
"invalid macvlan mode \"{}\"",
name
))),
}
}

Expand Down

0 comments on commit 0cc2556

Please sign in to comment.