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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//! Ethernet Context Information Element
//!
//! The Ethernet Context Information IE is a grouped IE used by SMF to provision MAC addresses
//! to the UPF for Ethernet PDU sessions. Per 3GPP TS 29.244 Section 7.5.4.21 Table 7.5.4.21-1
//! (IE type 254), this IE is used in PFCP Session Modification Request messages.
//!
//! NOTE: This IE contains only MAC Addresses Detected (for provisioning from SMF to UPF).
//! For reporting MAC address events from UPF to SMF, see Ethernet Traffic Information IE (143).
use crate::error::PfcpError;
use crate::ie::mac_addresses_detected::MacAddressesDetected;
use crate::ie::{Ie, IeType};
/// Ethernet Context Information (Grouped IE)
///
/// Used by SMF to provision MAC addresses to UPF for Ethernet PDU sessions.
///
/// # 3GPP Reference
/// 3GPP TS 29.244 Section 7.5.4.21 Table 7.5.4.21-1 (IE Type 254)
///
/// # Structure (Grouped IE containing):
/// - MAC Addresses Detected (mandatory when IE is present, may appear multiple times)
///
/// Per spec: "Several IEs with the same IE type may be present to provision multiple
/// lists of MAC addresses (e.g. with different V-LAN tags)."
///
/// # Direction
/// SMF → UPF (provisioning)
///
/// # Examples
///
/// ```
/// use rs_pfcp::ie::ethernet_context_information::{EthernetContextInformation, EthernetContextInformationBuilder};
/// use rs_pfcp::ie::mac_addresses_detected::MacAddressesDetected;
///
/// // Provision single MAC address
/// let mac1 = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
/// let detected = MacAddressesDetected::new(vec![mac1]).unwrap();
///
/// let context = EthernetContextInformationBuilder::new()
/// .add_mac_addresses_detected(detected)
/// .build()
/// .unwrap();
///
/// // Provision multiple MAC address lists (e.g., different VLANs)
/// let vlan100_macs = MacAddressesDetected::new(vec![
/// [0x00, 0x11, 0x22, 0x33, 0x44, 0x55]
/// ]).unwrap();
/// let vlan200_macs = MacAddressesDetected::new(vec![
/// [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF]
/// ]).unwrap();
///
/// let context2 = EthernetContextInformationBuilder::new()
/// .add_mac_addresses_detected(vlan100_macs)
/// .add_mac_addresses_detected(vlan200_macs)
/// .build()
/// .unwrap();
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EthernetContextInformation {
/// MAC Addresses Detected (mandatory when IE is present, multiple instances allowed)
/// Per 3GPP TS 29.244 Table 7.5.4.21-1
pub mac_addresses_detected: Vec<MacAddressesDetected>,
}
impl EthernetContextInformation {
/// Create a new Ethernet Context Information with MAC addresses
///
/// # Arguments
/// * `mac_addresses_detected` - List of MAC Addresses Detected IEs (at least one required)
///
/// # Example
/// ```
/// use rs_pfcp::ie::ethernet_context_information::EthernetContextInformation;
/// use rs_pfcp::ie::mac_addresses_detected::MacAddressesDetected;
///
/// let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
/// let detected = MacAddressesDetected::new(vec![mac]).unwrap();
/// let context = EthernetContextInformation::new(vec![detected]);
/// ```
pub fn new(mac_addresses_detected: Vec<MacAddressesDetected>) -> Self {
EthernetContextInformation {
mac_addresses_detected,
}
}
/// Marshal Ethernet Context Information to bytes (grouped IE format)
pub fn marshal(&self) -> Vec<u8> {
let mut data = Vec::new();
// Marshal all MAC Addresses Detected IEs
for detected in &self.mac_addresses_detected {
data.extend_from_slice(&detected.to_ie().marshal());
}
data
}
/// Unmarshal Ethernet Context Information from bytes
///
/// # Arguments
/// * `payload` - Grouped IE payload containing child IEs
///
/// # Errors
/// Returns error if no MAC Addresses Detected IE is present (mandatory per spec)
pub fn unmarshal(payload: &[u8]) -> Result<Self, PfcpError> {
let mut mac_addresses_detected = Vec::new();
let mut offset = 0;
while offset < payload.len() {
let ie = Ie::unmarshal(&payload[offset..])?;
match ie.ie_type {
IeType::MacAddressesDetected => {
mac_addresses_detected.push(MacAddressesDetected::unmarshal(&ie.payload)?);
}
_ => {
// Ignore unknown IEs for forward compatibility
}
}
offset += ie.len() as usize;
}
// Validate that at least one MAC Addresses Detected IE is present (mandatory per spec)
if mac_addresses_detected.is_empty() {
return Err(PfcpError::MissingMandatoryIe {
ie_type: IeType::MacAddressesDetected,
message_type: None,
parent_ie: Some(IeType::EthernetContextInformation),
});
}
Ok(EthernetContextInformation {
mac_addresses_detected,
})
}
/// Convert to generic IE
pub fn to_ie(&self) -> Ie {
Ie::new(IeType::EthernetContextInformation, self.marshal())
}
}
/// Builder for Ethernet Context Information
///
/// Provides an ergonomic way to construct Ethernet Context Information IEs.
///
/// # Examples
///
/// ```
/// use rs_pfcp::ie::ethernet_context_information::EthernetContextInformationBuilder;
/// use rs_pfcp::ie::mac_addresses_detected::MacAddressesDetected;
///
/// // Provision single MAC address list
/// let mac1 = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
/// let detected = MacAddressesDetected::new(vec![mac1]).unwrap();
///
/// let context = EthernetContextInformationBuilder::new()
/// .add_mac_addresses_detected(detected)
/// .build()
/// .unwrap();
///
/// // Provision multiple MAC address lists (e.g., different VLANs)
/// let vlan100 = MacAddressesDetected::new(vec![[0x00, 0x11, 0x22, 0x33, 0x44, 0x55]]).unwrap();
/// let vlan200 = MacAddressesDetected::new(vec![[0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF]]).unwrap();
///
/// let context2 = EthernetContextInformationBuilder::new()
/// .add_mac_addresses_detected(vlan100)
/// .add_mac_addresses_detected(vlan200)
/// .build()
/// .unwrap();
/// ```
#[derive(Debug, Clone, Default)]
pub struct EthernetContextInformationBuilder {
mac_addresses_detected: Vec<MacAddressesDetected>,
}
impl EthernetContextInformationBuilder {
/// Create a new builder
pub fn new() -> Self {
EthernetContextInformationBuilder {
mac_addresses_detected: Vec::new(),
}
}
/// Add a MAC Addresses Detected IE
///
/// Can be called multiple times to add multiple MAC address lists
/// (e.g., for different VLANs)
pub fn add_mac_addresses_detected(mut self, detected: MacAddressesDetected) -> Self {
self.mac_addresses_detected.push(detected);
self
}
/// Set all MAC Addresses Detected IEs at once
pub fn mac_addresses_detected(mut self, detected: Vec<MacAddressesDetected>) -> Self {
self.mac_addresses_detected = detected;
self
}
/// Build the Ethernet Context Information
///
/// # Errors
/// Returns error if no MAC Addresses Detected IE has been added (mandatory per spec)
pub fn build(self) -> Result<EthernetContextInformation, PfcpError> {
if self.mac_addresses_detected.is_empty() {
return Err(PfcpError::validation_error(
"EthernetContextInformationBuilder",
"mac_addresses_detected",
"Ethernet Context Information requires at least one MAC Addresses Detected IE per 3GPP TS 29.244 Table 7.5.4.21-1",
));
}
Ok(EthernetContextInformation {
mac_addresses_detected: self.mac_addresses_detected,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ethernet_context_information_new() {
let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
let detected = MacAddressesDetected::new(vec![mac]).unwrap();
let context = EthernetContextInformation::new(vec![detected.clone()]);
assert_eq!(context.mac_addresses_detected.len(), 1);
assert_eq!(context.mac_addresses_detected[0], detected);
}
#[test]
fn test_ethernet_context_information_builder_empty() {
// Builder should fail when no MAC addresses added (mandatory per spec)
let result = EthernetContextInformationBuilder::new().build();
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("at least one"));
}
#[test]
fn test_ethernet_context_information_builder_with_detected() {
let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
let detected = MacAddressesDetected::new(vec![mac]).unwrap();
let context = EthernetContextInformationBuilder::new()
.add_mac_addresses_detected(detected.clone())
.build()
.unwrap();
assert_eq!(context.mac_addresses_detected.len(), 1);
assert_eq!(context.mac_addresses_detected[0], detected);
}
#[test]
fn test_ethernet_context_information_builder_multiple_detected() {
let mac1 = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
let mac2 = [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF];
let detected1 = MacAddressesDetected::new(vec![mac1]).unwrap();
let detected2 = MacAddressesDetected::new(vec![mac2]).unwrap();
let context = EthernetContextInformationBuilder::new()
.add_mac_addresses_detected(detected1.clone())
.add_mac_addresses_detected(detected2.clone())
.build()
.unwrap();
assert_eq!(context.mac_addresses_detected.len(), 2);
assert_eq!(context.mac_addresses_detected[0], detected1);
assert_eq!(context.mac_addresses_detected[1], detected2);
}
#[test]
fn test_ethernet_context_information_unmarshal_empty() {
// Empty payload should fail (mandatory MAC Addresses Detected per spec)
let result = EthernetContextInformation::unmarshal(&[]);
assert!(result.is_err());
assert!(matches!(
result.unwrap_err(),
PfcpError::MissingMandatoryIe { .. }
));
}
#[test]
fn test_ethernet_context_information_round_trip_with_detected() {
let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
let detected = MacAddressesDetected::new(vec![mac]).unwrap();
let original = EthernetContextInformationBuilder::new()
.add_mac_addresses_detected(detected)
.build()
.unwrap();
let marshaled = original.marshal();
let unmarshaled = EthernetContextInformation::unmarshal(&marshaled).unwrap();
assert_eq!(original, unmarshaled);
}
#[test]
fn test_ethernet_context_information_round_trip_comprehensive() {
let mac1 = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
let mac2 = [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF];
let mac3 = [0x11, 0x22, 0x33, 0x44, 0x55, 0x66];
let detected1 = MacAddressesDetected::new(vec![mac1, mac2]).unwrap();
let detected2 = MacAddressesDetected::new(vec![mac3]).unwrap();
let original = EthernetContextInformationBuilder::new()
.add_mac_addresses_detected(detected1)
.add_mac_addresses_detected(detected2)
.build()
.unwrap();
let marshaled = original.marshal();
let unmarshaled = EthernetContextInformation::unmarshal(&marshaled).unwrap();
assert_eq!(original, unmarshaled);
}
#[test]
fn test_ethernet_context_information_to_ie() {
let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
let detected = MacAddressesDetected::new(vec![mac]).unwrap();
let context = EthernetContextInformationBuilder::new()
.add_mac_addresses_detected(detected)
.build()
.unwrap();
let ie = context.to_ie();
assert_eq!(ie.ie_type, IeType::EthernetContextInformation);
// Verify IE can be unmarshaled
let parsed = EthernetContextInformation::unmarshal(&ie.payload).unwrap();
assert_eq!(context, parsed);
}
#[test]
fn test_ethernet_context_information_scenarios() {
// Scenario 1: Provision single MAC address
let mac = [0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5E];
let detected = MacAddressesDetected::new(vec![mac]).unwrap();
let context1 = EthernetContextInformationBuilder::new()
.add_mac_addresses_detected(detected)
.build()
.unwrap();
assert_eq!(context1.mac_addresses_detected.len(), 1);
// Scenario 2: Provision multiple MAC address lists (different VLANs)
let vlan100_macs = vec![
[0x11, 0x22, 0x33, 0x44, 0x55, 0x66],
[0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC],
];
let vlan200_macs = vec![[0xDD, 0xEE, 0xFF, 0x00, 0x11, 0x22]];
let detected_vlan100 = MacAddressesDetected::new(vlan100_macs).unwrap();
let detected_vlan200 = MacAddressesDetected::new(vlan200_macs).unwrap();
let context2 = EthernetContextInformationBuilder::new()
.add_mac_addresses_detected(detected_vlan100.clone())
.add_mac_addresses_detected(detected_vlan200.clone())
.build()
.unwrap();
assert_eq!(context2.mac_addresses_detected.len(), 2);
assert_eq!(context2.mac_addresses_detected[0].count(), 2);
assert_eq!(context2.mac_addresses_detected[1].count(), 1);
}
}