|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +use std::collections::HashMap; |
| 4 | + |
| 5 | +use iproute_rs::{CliError, mac_to_string}; |
| 6 | +use rtnetlink::{ |
| 7 | + LinkHsr, LinkMessageBuilder, |
| 8 | + packet_route::link::{HsrProtocol, InfoHsr}, |
| 9 | +}; |
| 10 | +use serde::Serialize; |
| 11 | + |
| 12 | +use super::parse::parse_u8; |
| 13 | +use crate::link::LinkBaseConf; |
| 14 | + |
| 15 | +#[derive(Serialize)] |
| 16 | +pub(crate) struct CliLinkInfoDataHsr { |
| 17 | + #[serde(skip)] |
| 18 | + port1_index: Option<u32>, |
| 19 | + #[serde(skip)] |
| 20 | + port2_index: Option<u32>, |
| 21 | + #[serde(skip)] |
| 22 | + interlink_index: Option<u32>, |
| 23 | + #[serde(rename = "slave1", skip_serializing_if = "Option::is_none")] |
| 24 | + port1: Option<String>, |
| 25 | + #[serde(rename = "slave2", skip_serializing_if = "Option::is_none")] |
| 26 | + port2: Option<String>, |
| 27 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 28 | + interlink: Option<String>, |
| 29 | + #[serde(skip_serializing_if = "Option::is_none", rename = "seq_nr")] |
| 30 | + sequence: Option<u16>, |
| 31 | + #[serde( |
| 32 | + skip_serializing_if = "Option::is_none", |
| 33 | + rename = "supervision_addr" |
| 34 | + )] |
| 35 | + supervision: Option<String>, |
| 36 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 37 | + proto: Option<u8>, |
| 38 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 39 | + version: Option<u8>, |
| 40 | +} |
| 41 | + |
| 42 | +impl CliLinkInfoDataHsr { |
| 43 | + pub(crate) fn resolve_link(&mut self, index_2_name: &HashMap<u32, String>) { |
| 44 | + let resolve = |idx: u32| { |
| 45 | + index_2_name |
| 46 | + .get(&idx) |
| 47 | + .cloned() |
| 48 | + .unwrap_or_else(|| format!("if{idx}")) |
| 49 | + }; |
| 50 | + self.port1 = self.port1_index.map(resolve); |
| 51 | + self.port2 = self.port2_index.map(resolve); |
| 52 | + self.interlink = self.interlink_index.map(resolve); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl From<&[InfoHsr]> for CliLinkInfoDataHsr { |
| 57 | + fn from(info: &[InfoHsr]) -> Self { |
| 58 | + let mut port1_index = None; |
| 59 | + let mut port2_index = None; |
| 60 | + let mut interlink_index = None; |
| 61 | + let mut sequence = None; |
| 62 | + let mut supervision = None; |
| 63 | + let mut proto = None; |
| 64 | + let mut version = None; |
| 65 | + |
| 66 | + for nla in info { |
| 67 | + match nla { |
| 68 | + InfoHsr::Port1(v) => port1_index = Some(*v), |
| 69 | + InfoHsr::Port2(v) => port2_index = Some(*v), |
| 70 | + InfoHsr::Interlink(v) => interlink_index = Some(*v), |
| 71 | + InfoHsr::SeqNr(v) => sequence = Some(*v), |
| 72 | + InfoHsr::SupervisionAddr(v) => { |
| 73 | + supervision = Some(mac_to_string(v)) |
| 74 | + } |
| 75 | + InfoHsr::Protocol(HsrProtocol::Hsr) => proto = Some(0), |
| 76 | + InfoHsr::Protocol(HsrProtocol::Prp) => proto = Some(1), |
| 77 | + InfoHsr::Protocol(HsrProtocol::Other(v)) => proto = Some(*v), |
| 78 | + InfoHsr::Version(v) => version = Some(*v), |
| 79 | + _ => (), |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + Self { |
| 84 | + port1_index, |
| 85 | + port2_index, |
| 86 | + interlink_index, |
| 87 | + port1: None, |
| 88 | + port2: None, |
| 89 | + interlink: None, |
| 90 | + sequence, |
| 91 | + supervision, |
| 92 | + proto, |
| 93 | + version, |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl std::fmt::Display for CliLinkInfoDataHsr { |
| 99 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 100 | + if let Some(v) = &self.port1 { |
| 101 | + write!(f, "slave1 {v}")?; |
| 102 | + } |
| 103 | + if let Some(v) = &self.port2 { |
| 104 | + write!(f, " slave2 {v}")?; |
| 105 | + } |
| 106 | + if let Some(v) = &self.interlink { |
| 107 | + write!(f, " interlink {v}")?; |
| 108 | + } |
| 109 | + if let Some(v) = self.sequence { |
| 110 | + write!(f, " sequence {v}")?; |
| 111 | + } |
| 112 | + if let Some(v) = &self.supervision { |
| 113 | + write!(f, " supervision {v}")?; |
| 114 | + } |
| 115 | + if let Some(v) = self.proto { |
| 116 | + write!(f, " proto {v}")?; |
| 117 | + } |
| 118 | + if let Some(v) = self.version { |
| 119 | + write!(f, " version {v}")?; |
| 120 | + } |
| 121 | + Ok(()) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +impl LinkBaseConf { |
| 126 | + pub(crate) async fn apply_hsr( |
| 127 | + &self, |
| 128 | + handle: &rtnetlink::Handle, |
| 129 | + ) -> Result<LinkMessageBuilder<LinkHsr>, CliError> { |
| 130 | + let mut builder = LinkHsr::new(&self.name); |
| 131 | + let mut has_port1 = false; |
| 132 | + let mut has_port2 = false; |
| 133 | + |
| 134 | + let mut iter = self.iface_specific.iter(); |
| 135 | + while let Some(key) = iter.next() { |
| 136 | + let mut next_val = || { |
| 137 | + iter.next().ok_or_else(|| { |
| 138 | + CliError::from(format!("hsr {key} requires a value")) |
| 139 | + }) |
| 140 | + }; |
| 141 | + match key.as_str() { |
| 142 | + "slave1" => { |
| 143 | + let v = next_val()?; |
| 144 | + let ifindex = self.get_ifindex_by_name(handle, v).await?; |
| 145 | + builder = builder.port1(ifindex); |
| 146 | + has_port1 = true; |
| 147 | + } |
| 148 | + "slave2" => { |
| 149 | + let v = next_val()?; |
| 150 | + let ifindex = self.get_ifindex_by_name(handle, v).await?; |
| 151 | + builder = builder.port2(ifindex); |
| 152 | + has_port2 = true; |
| 153 | + } |
| 154 | + "interlink" => { |
| 155 | + let v = next_val()?; |
| 156 | + let ifindex = self.get_ifindex_by_name(handle, v).await?; |
| 157 | + builder = builder.interlink(ifindex); |
| 158 | + } |
| 159 | + "supervision" => { |
| 160 | + let v = next_val()?; |
| 161 | + builder = builder.supervision(parse_u8(v, "supervision")?); |
| 162 | + } |
| 163 | + "version" => { |
| 164 | + let v = next_val()?; |
| 165 | + builder = builder.version(parse_u8(v, "version")?); |
| 166 | + } |
| 167 | + "proto" => { |
| 168 | + let v = next_val()?; |
| 169 | + let proto: u8 = parse_u8(v, "proto")?; |
| 170 | + builder = builder.protocol(proto.into()); |
| 171 | + } |
| 172 | + _ => { |
| 173 | + return Err(CliError::from(format!( |
| 174 | + "Unknown hsr argument: {key}" |
| 175 | + ))); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + if !has_port1 || !has_port2 { |
| 181 | + return Err(CliError::from( |
| 182 | + "hsr requires slave1 and slave2 arguments", |
| 183 | + )); |
| 184 | + } |
| 185 | + |
| 186 | + Ok(builder) |
| 187 | + } |
| 188 | +} |
0 commit comments