55// Copyright 2024 Oxide Computer Company
66
77use std:: collections:: BTreeSet ;
8- use std:: fmt;
98use std:: iter:: FromIterator ;
109use std:: str:: FromStr ;
1110
12- use schemars:: JsonSchema ;
13- use serde:: Deserialize ;
14- use serde:: Serialize ;
1511use slog:: o;
1612use slog:: Drain ;
17- use thiserror:: Error ;
1813
1914pub 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 ) ]
21332pub enum LogFormat {
21433 Human ,
0 commit comments