1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//! Ethernet PDU Session Information Element
//!
//! The Ethernet PDU Session Information IE indicates whether the Ethernet PDU session
//! is carrying Ethernet frames or untagged Ethernet frames. Per 3GPP TS 29.244 Section 8.2.102,
//! this IE provides information about the Ethernet encapsulation used in the session.
use crate::error::PfcpError;
use crate::ie::{Ie, IeType};
/// Ethernet PDU Session Information
///
/// Indicates the Ethernet PDU session encapsulation type.
///
/// # 3GPP Reference
/// 3GPP TS 29.244 Section 8.2.102
///
/// # Structure
/// - Octet 5: Flags (currently only ETHI flag defined)
///
/// ```text
/// Bit 1: ETHI (Ethernet Header Indication)
/// 0 = Ethernet header is present
/// 1 = Ethernet header is not present (untagged)
/// Bits 2-8: Spare (set to 0)
/// ```
///
/// # Examples
///
/// ```
/// use rs_pfcp::ie::ethernet_pdu_session_information::EthernetPduSessionInformation;
///
/// // Create with Ethernet header present
/// let with_header = EthernetPduSessionInformation::with_ethernet_header();
/// assert!(!with_header.is_untagged());
///
/// // Create without Ethernet header (untagged)
/// let untagged = EthernetPduSessionInformation::untagged();
/// assert!(untagged.is_untagged());
///
/// // Marshal and unmarshal
/// let bytes = with_header.marshal();
/// let parsed = EthernetPduSessionInformation::unmarshal(&bytes).unwrap();
/// assert_eq!(with_header, parsed);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EthernetPduSessionInformation {
/// ETHI flag - Ethernet Header Indication
/// false = Ethernet header present, true = no Ethernet header (untagged)
ethi: bool,
}
impl EthernetPduSessionInformation {
/// ETHI flag bit mask
const ETHI_FLAG: u8 = 0x01;
/// Create new Ethernet PDU Session Information
///
/// # Arguments
/// * `untagged` - True if Ethernet header is not present (untagged), false otherwise
///
/// # Example
/// ```
/// use rs_pfcp::ie::ethernet_pdu_session_information::EthernetPduSessionInformation;
///
/// let info = EthernetPduSessionInformation::new(false);
/// assert!(!info.is_untagged());
/// ```
pub fn new(untagged: bool) -> Self {
EthernetPduSessionInformation { ethi: untagged }
}
/// Create Ethernet PDU session with Ethernet header present
///
/// # Example
/// ```
/// use rs_pfcp::ie::ethernet_pdu_session_information::EthernetPduSessionInformation;
///
/// let info = EthernetPduSessionInformation::with_ethernet_header();
/// assert!(!info.is_untagged());
/// assert!(info.has_ethernet_header());
/// ```
pub fn with_ethernet_header() -> Self {
EthernetPduSessionInformation::new(false)
}
/// Create untagged Ethernet PDU session (no Ethernet header)
///
/// # Example
/// ```
/// use rs_pfcp::ie::ethernet_pdu_session_information::EthernetPduSessionInformation;
///
/// let info = EthernetPduSessionInformation::untagged();
/// assert!(info.is_untagged());
/// assert!(!info.has_ethernet_header());
/// ```
pub fn untagged() -> Self {
EthernetPduSessionInformation::new(true)
}
/// Check if Ethernet header is not present (untagged)
///
/// # Example
/// ```
/// use rs_pfcp::ie::ethernet_pdu_session_information::EthernetPduSessionInformation;
///
/// let untagged = EthernetPduSessionInformation::untagged();
/// assert!(untagged.is_untagged());
/// ```
pub fn is_untagged(&self) -> bool {
self.ethi
}
/// Check if Ethernet header is present
///
/// # Example
/// ```
/// use rs_pfcp::ie::ethernet_pdu_session_information::EthernetPduSessionInformation;
///
/// let with_header = EthernetPduSessionInformation::with_ethernet_header();
/// assert!(with_header.has_ethernet_header());
/// ```
pub fn has_ethernet_header(&self) -> bool {
!self.ethi
}
/// Marshal Ethernet PDU Session Information to bytes
///
/// # Returns
/// 1-byte array with flags
pub fn marshal(&self) -> [u8; 1] {
let mut flags = 0u8;
if self.ethi {
flags |= Self::ETHI_FLAG;
}
[flags]
}
/// Unmarshal Ethernet PDU Session Information from bytes
///
/// # Arguments
/// * `data` - Byte slice containing session info data (must be at least 1 byte)
///
/// # Errors
/// Returns error if data is too short
///
/// # Example
/// ```
/// use rs_pfcp::ie::ethernet_pdu_session_information::EthernetPduSessionInformation;
///
/// let info = EthernetPduSessionInformation::with_ethernet_header();
/// let bytes = info.marshal();
/// let parsed = EthernetPduSessionInformation::unmarshal(&bytes).unwrap();
/// assert_eq!(info, parsed);
/// ```
pub fn unmarshal(data: &[u8]) -> Result<Self, PfcpError> {
if data.is_empty() {
return Err(PfcpError::invalid_length(
"Ethernet PDU Session Information",
IeType::EthernetPduSessionInformation,
1,
0,
));
}
let ethi = (data[0] & Self::ETHI_FLAG) != 0;
Ok(EthernetPduSessionInformation { ethi })
}
/// Convert to generic IE
///
/// # Example
/// ```
/// use rs_pfcp::ie::ethernet_pdu_session_information::EthernetPduSessionInformation;
/// use rs_pfcp::ie::IeType;
///
/// let info = EthernetPduSessionInformation::with_ethernet_header();
/// let ie = info.to_ie();
/// assert_eq!(ie.ie_type, IeType::EthernetPduSessionInformation);
/// ```
pub fn to_ie(&self) -> Ie {
Ie::new(
IeType::EthernetPduSessionInformation,
self.marshal().to_vec(),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ethernet_pdu_session_information_new() {
let with_header = EthernetPduSessionInformation::new(false);
assert!(!with_header.is_untagged());
assert!(with_header.has_ethernet_header());
let untagged = EthernetPduSessionInformation::new(true);
assert!(untagged.is_untagged());
assert!(!untagged.has_ethernet_header());
}
#[test]
fn test_ethernet_pdu_session_information_with_header() {
let info = EthernetPduSessionInformation::with_ethernet_header();
assert!(!info.is_untagged());
assert!(info.has_ethernet_header());
}
#[test]
fn test_ethernet_pdu_session_information_untagged() {
let info = EthernetPduSessionInformation::untagged();
assert!(info.is_untagged());
assert!(!info.has_ethernet_header());
}
#[test]
fn test_ethernet_pdu_session_information_marshal() {
let with_header = EthernetPduSessionInformation::with_ethernet_header();
let bytes = with_header.marshal();
assert_eq!(bytes.len(), 1);
assert_eq!(bytes[0], 0x00); // No flags set
let untagged = EthernetPduSessionInformation::untagged();
let bytes_untagged = untagged.marshal();
assert_eq!(bytes_untagged.len(), 1);
assert_eq!(bytes_untagged[0], 0x01); // ETHI flag set
}
#[test]
fn test_ethernet_pdu_session_information_unmarshal_with_header() {
let data = [0x00]; // No flags set
let info = EthernetPduSessionInformation::unmarshal(&data).unwrap();
assert!(!info.is_untagged());
assert!(info.has_ethernet_header());
}
#[test]
fn test_ethernet_pdu_session_information_unmarshal_untagged() {
let data = [0x01]; // ETHI flag set
let info = EthernetPduSessionInformation::unmarshal(&data).unwrap();
assert!(info.is_untagged());
assert!(!info.has_ethernet_header());
}
#[test]
fn test_ethernet_pdu_session_information_unmarshal_with_spare_bits() {
// Test with spare bits set (should be ignored)
let data = [0xFF]; // All bits set
let info = EthernetPduSessionInformation::unmarshal(&data).unwrap();
assert!(info.is_untagged()); // ETHI bit is set
}
#[test]
fn test_ethernet_pdu_session_information_unmarshal_empty() {
let result = EthernetPduSessionInformation::unmarshal(&[]);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(matches!(err, PfcpError::InvalidLength { .. }));
}
#[test]
fn test_ethernet_pdu_session_information_round_trip() {
let test_cases = vec![true, false];
for untagged in test_cases {
let original = EthernetPduSessionInformation::new(untagged);
let marshaled = original.marshal();
let unmarshaled = EthernetPduSessionInformation::unmarshal(&marshaled).unwrap();
assert_eq!(original, unmarshaled, "Failed for untagged={}", untagged);
}
}
#[test]
fn test_ethernet_pdu_session_information_to_ie() {
let info = EthernetPduSessionInformation::with_ethernet_header();
let ie = info.to_ie();
assert_eq!(ie.ie_type, IeType::EthernetPduSessionInformation);
assert_eq!(ie.payload.len(), 1);
assert_eq!(ie.payload[0], 0x00);
// Verify IE can be unmarshaled
let parsed = EthernetPduSessionInformation::unmarshal(&ie.payload).unwrap();
assert_eq!(info, parsed);
}
#[test]
fn test_ethernet_pdu_session_information_scenarios() {
// Scenario 1: Standard Ethernet PDU session with 802.3 header
let standard = EthernetPduSessionInformation::with_ethernet_header();
assert!(standard.has_ethernet_header());
assert!(!standard.is_untagged());
// Scenario 2: Untagged Ethernet PDU session (raw payload)
let raw = EthernetPduSessionInformation::untagged();
assert!(!raw.has_ethernet_header());
assert!(raw.is_untagged());
// Scenario 3: VLAN-tagged Ethernet frames
let vlan_tagged = EthernetPduSessionInformation::with_ethernet_header();
assert!(vlan_tagged.has_ethernet_header());
// Scenario 4: Layer 3 encapsulation (no Ethernet header)
let l3_only = EthernetPduSessionInformation::untagged();
assert!(l3_only.is_untagged());
}
#[test]
fn test_ethernet_pdu_session_information_clone_copy() {
let info1 = EthernetPduSessionInformation::with_ethernet_header();
let info2 = info1;
assert_eq!(info1, info2);
let info3 = info1;
assert_eq!(info1, info3);
}
}