Skip to content

Commit d94d060

Browse files
authored
Refactor to make parser accessible (#25)
1 parent 2d7113e commit d94d060

File tree

17 files changed

+358
-339
lines changed

17 files changed

+358
-339
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ members = [
55
"common",
66
"lldpd-client",
77
"lldpd",
8+
"protocol",
89
"xtask",
910
]
1011

@@ -14,6 +15,7 @@ resolver = "2"
1415

1516
# intra-package dependencies
1617
lldpd-client = { path = "lldpd-client" }
18+
protocol = { path = "protocol" }
1719
common = { path = "common" }
1820

1921
# oxide dependencies from github

common/src/lib.rs

Lines changed: 0 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@
55
// Copyright 2024 Oxide Computer Company
66

77
use std::collections::BTreeSet;
8-
use std::fmt;
98
use std::iter::FromIterator;
109
use std::str::FromStr;
1110

12-
use schemars::JsonSchema;
13-
use serde::Deserialize;
14-
use serde::Serialize;
1511
use slog::o;
1612
use slog::Drain;
17-
use thiserror::Error;
1813

1914
pub const DEFAULT_LLDPD_PORT: u16 = 12230;
2015

@@ -33,182 +28,6 @@ where
3328
)
3429
}
3530

36-
/// An EUI-48 MAC address, used for layer-2 addressing.
37-
#[derive(
38-
Clone,
39-
Copy,
40-
Deserialize,
41-
JsonSchema,
42-
Serialize,
43-
Eq,
44-
PartialEq,
45-
Ord,
46-
PartialOrd,
47-
Hash,
48-
)]
49-
pub struct MacAddr {
50-
a: [u8; 6],
51-
}
52-
53-
impl From<[u8; 6]> for MacAddr {
54-
fn from(a: [u8; 6]) -> Self {
55-
Self { a }
56-
}
57-
}
58-
59-
impl From<dpd_client::types::MacAddr> for MacAddr {
60-
fn from(a: dpd_client::types::MacAddr) -> Self {
61-
a.a.into()
62-
}
63-
}
64-
65-
impl MacAddr {
66-
pub const ZERO: Self = MacAddr {
67-
a: [0, 0, 0, 0, 0, 0],
68-
};
69-
70-
/// Create a new MAC address from octets in network byte order.
71-
pub fn new(o0: u8, o1: u8, o2: u8, o3: u8, o4: u8, o5: u8) -> MacAddr {
72-
MacAddr {
73-
a: [o0, o1, o2, o3, o4, o5],
74-
}
75-
}
76-
77-
/// Create a new MAC address from a slice of bytes in network byte order.
78-
///
79-
/// # Panics
80-
///
81-
/// Panics if the slice is fewer than 6 octets.
82-
///
83-
/// Note that any further octets are ignored.
84-
pub fn from_slice(s: &[u8]) -> MacAddr {
85-
MacAddr::new(s[0], s[1], s[2], s[3], s[4], s[5])
86-
}
87-
88-
/// Convert `self` to an array of bytes in network byte order.
89-
pub fn to_vec(self) -> Vec<u8> {
90-
vec![
91-
self.a[0], self.a[1], self.a[2], self.a[3], self.a[4], self.a[5],
92-
]
93-
}
94-
95-
/// Return `true` if `self` is the null MAC address, all zeros.
96-
pub fn is_null(self) -> bool {
97-
const EMPTY: MacAddr = MacAddr {
98-
a: [0, 0, 0, 0, 0, 0],
99-
};
100-
101-
self == EMPTY
102-
}
103-
104-
/// Generate an EUI-64 ID from the mac address, following the process
105-
/// desribed in RFC 2464, section 4.
106-
pub fn to_eui64(self) -> [u8; 8] {
107-
[
108-
self.a[0] ^ 0x2,
109-
self.a[1],
110-
self.a[2],
111-
0xff,
112-
0xfe,
113-
self.a[3],
114-
self.a[4],
115-
self.a[5],
116-
]
117-
}
118-
}
119-
120-
#[derive(Error, Debug, Clone)]
121-
pub enum MacError {
122-
/// Too few octets to be a valid MAC address
123-
#[error("Too few octets")]
124-
TooShort,
125-
/// Too many octets to be a valid MAC address
126-
#[error("Too many octets")]
127-
TooLong,
128-
/// Found an octet with a non-hexadecimal character or invalid separator
129-
#[error("Invalid octect")]
130-
InvalidOctet,
131-
}
132-
133-
impl FromStr for MacAddr {
134-
type Err = MacError;
135-
136-
fn from_str(s: &str) -> Result<Self, MacError> {
137-
let v: Vec<&str> = s.split(':').collect();
138-
139-
match v.len().cmp(&6) {
140-
std::cmp::Ordering::Less => Err(MacError::TooShort),
141-
std::cmp::Ordering::Greater => Err(MacError::TooLong),
142-
std::cmp::Ordering::Equal => {
143-
let mut m = MacAddr { a: [0u8; 6] };
144-
for (i, octet) in v.iter().enumerate() {
145-
m.a[i] = u8::from_str_radix(octet, 16)
146-
.map_err(|_| MacError::InvalidOctet)?;
147-
}
148-
Ok(m)
149-
}
150-
}
151-
}
152-
}
153-
154-
impl fmt::Display for MacAddr {
155-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
156-
write!(
157-
f,
158-
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
159-
self.a[0], self.a[1], self.a[2], self.a[3], self.a[4], self.a[5]
160-
)
161-
}
162-
}
163-
164-
impl fmt::Debug for MacAddr {
165-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
166-
write!(
167-
f,
168-
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
169-
self.a[0], self.a[1], self.a[2], self.a[3], self.a[4], self.a[5]
170-
)
171-
}
172-
}
173-
174-
impl From<MacAddr> for [u8; 6] {
175-
fn from(mac: MacAddr) -> [u8; 6] {
176-
mac.a
177-
}
178-
}
179-
180-
impl From<MacAddr> for u64 {
181-
fn from(mac: MacAddr) -> u64 {
182-
((mac.a[0] as u64) << 40)
183-
| ((mac.a[1] as u64) << 32)
184-
| ((mac.a[2] as u64) << 24)
185-
| ((mac.a[3] as u64) << 16)
186-
| ((mac.a[4] as u64) << 8)
187-
| (mac.a[5] as u64)
188-
}
189-
}
190-
191-
impl From<&MacAddr> for u64 {
192-
fn from(mac: &MacAddr) -> u64 {
193-
From::from(*mac)
194-
}
195-
}
196-
197-
impl From<u64> for MacAddr {
198-
fn from(x: u64) -> Self {
199-
MacAddr {
200-
a: [
201-
((x >> 40) & 0xff) as u8,
202-
((x >> 32) & 0xff) as u8,
203-
((x >> 24) & 0xff) as u8,
204-
((x >> 16) & 0xff) as u8,
205-
((x >> 8) & 0xff) as u8,
206-
(x & 0xff) as u8,
207-
],
208-
}
209-
}
210-
}
211-
21231
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
21332
pub enum LogFormat {
21433
Human,

lldpd/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ smf = [ "dep:smf-rs" ]
1212
anyhow.workspace = true
1313
chrono.workspace = true
1414
common.workspace = true
15+
protocol.workspace = true
1516
dpd-client.workspace = true
1617
dropshot.workspace = true
1718
http.workspace = true

lldpd/src/api_server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ use slog::info;
3737
use slog::o;
3838

3939
use crate::interfaces;
40-
use crate::protocol;
4140
use crate::types;
4241
use crate::Global;
4342
use crate::LldpdError;
43+
use protocol::types as protocol;
4444

4545
type ApiServer = dropshot::HttpServer<Arc<Global>>;
4646

lldpd/src/dendrite.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use slog::info;
1010
use crate::Global;
1111
use crate::LldpdError;
1212
use crate::LldpdResult;
13-
use common::MacAddr;
1413
use dpd_client::types::LinkId;
1514
use dpd_client::types::PortId;
1615
use dpd_client::Client;
1716
use dpd_client::ClientState;
17+
use protocol::macaddr::MacAddr;
1818

1919
fn parse_link_name(name: &str) -> LldpdResult<(PortId, LinkId)> {
2020
let Some((port_id, link_id)) = name.split_once('/') else {
@@ -45,8 +45,10 @@ pub async fn dpd_tfport(
4545
.await
4646
.map_err(|e| LldpdError::DpdClientError(e.to_string()))?;
4747
let iface = format!("tfport{port_id}_{link_id}");
48-
let mac = link_info.into_inner().address;
49-
Ok((iface, mac.into()))
48+
let dpd_mac = link_info.into_inner().address;
49+
let mac = dpd_mac.a.into();
50+
51+
Ok((iface, mac))
5052
}
5153

5254
async fn dpd_version(log: &slog::Logger, client: &Client) -> String {

0 commit comments

Comments
 (0)