rs-pfcp 0.4.0

High-performance Rust implementation of PFCP (Packet Forwarding Control Protocol) for 5G networks with 100% 3GPP TS 29.244 Release 18 compliance
Documentation
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! ForwardingParameters IE and its sub-IEs.

use crate::error::PfcpError;
use crate::ie::{
    create_traffic_endpoint::TrafficEndpointId, destination_interface::DestinationInterface,
    header_enrichment::HeaderEnrichment, marshal_ies, network_instance::NetworkInstance,
    outer_header_creation::OuterHeaderCreation, proxying::Proxying,
    three_gpp_interface_type::ThreeGppInterfaceTypeIe,
    transport_level_marking::TransportLevelMarking, Ie, IeIterator, IeType,
};

/// Represents the Forwarding Parameters.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ForwardingParameters {
    pub destination_interface: DestinationInterface,
    pub network_instance: Option<NetworkInstance>,
    pub transport_level_marking: Option<TransportLevelMarking>,
    pub outer_header_creation: Option<OuterHeaderCreation>,
    pub traffic_endpoint_id: Option<TrafficEndpointId>,
    pub proxying: Option<Proxying>,
    pub three_gpp_interface_type: Option<ThreeGppInterfaceTypeIe>,
    pub header_enrichment: Option<HeaderEnrichment>,
    pub redundant_transmission_forwarding_parameters: Option<Ie>,
}

impl ForwardingParameters {
    /// Creates a new Forwarding Parameters IE.
    pub fn new(destination_interface: DestinationInterface) -> Self {
        ForwardingParameters {
            destination_interface,
            network_instance: None,
            transport_level_marking: None,
            outer_header_creation: None,
            traffic_endpoint_id: None,
            proxying: None,
            three_gpp_interface_type: None,
            header_enrichment: None,
            redundant_transmission_forwarding_parameters: None,
        }
    }

    /// Adds a Network Instance to the Forwarding Parameters.
    pub fn with_network_instance(mut self, network_instance: NetworkInstance) -> Self {
        self.network_instance = Some(network_instance);
        self
    }

    /// Adds a Transport Level Marking to the Forwarding Parameters.
    pub fn with_transport_level_marking(
        mut self,
        transport_level_marking: TransportLevelMarking,
    ) -> Self {
        self.transport_level_marking = Some(transport_level_marking);
        self
    }

    /// Adds Outer Header Creation to the Forwarding Parameters.
    pub fn with_outer_header_creation(
        mut self,
        outer_header_creation: OuterHeaderCreation,
    ) -> Self {
        self.outer_header_creation = Some(outer_header_creation);
        self
    }

    /// Adds Traffic Endpoint ID to the Forwarding Parameters.
    ///
    /// Traffic Endpoint ID identifies a specific traffic endpoint within a PDU session
    /// for multi-access scenarios. This enables traffic steering between different access
    /// paths (e.g., WiFi and cellular) within the same session.
    ///
    /// # Arguments
    /// * `traffic_endpoint_id` - The traffic endpoint identifier (0-255)
    ///
    /// # 3GPP Reference
    /// 3GPP TS 29.244 Section 8.2.92 - IE Type 131
    ///
    /// # Example
    /// ```
    /// use rs_pfcp::ie::forwarding_parameters::ForwardingParameters;
    /// use rs_pfcp::ie::destination_interface::{DestinationInterface, Interface};
    /// use rs_pfcp::ie::create_traffic_endpoint::TrafficEndpointId;
    ///
    /// let params = ForwardingParameters::new(DestinationInterface::new(Interface::Access))
    ///     .with_traffic_endpoint_id(TrafficEndpointId::new(42));
    ///
    /// assert_eq!(params.traffic_endpoint_id.unwrap().id, 42);
    /// ```
    pub fn with_traffic_endpoint_id(mut self, traffic_endpoint_id: TrafficEndpointId) -> Self {
        self.traffic_endpoint_id = Some(traffic_endpoint_id);
        self
    }

    /// Adds Proxying to the Forwarding Parameters.
    pub fn with_proxying(mut self, proxying: Proxying) -> Self {
        self.proxying = Some(proxying);
        self
    }

    /// Adds 3GPP Interface Type to the Forwarding Parameters.
    pub fn with_three_gpp_interface_type(
        mut self,
        interface_type: ThreeGppInterfaceTypeIe,
    ) -> Self {
        self.three_gpp_interface_type = Some(interface_type);
        self
    }

    /// Adds Header Enrichment to the Forwarding Parameters.
    pub fn with_header_enrichment(mut self, header_enrichment: HeaderEnrichment) -> Self {
        self.header_enrichment = Some(header_enrichment);
        self
    }

    /// Marshals the Forwarding Parameters into a byte vector.
    pub fn marshal(&self) -> Vec<u8> {
        let mut ies = Vec::new();
        ies.push(self.destination_interface.to_ie());
        if let Some(ref ni) = self.network_instance {
            ies.push(ni.to_ie());
        }
        if let Some(ref tlm) = self.transport_level_marking {
            ies.push(tlm.to_ie());
        }
        if let Some(ref ohc) = self.outer_header_creation {
            ies.push(ohc.to_ie());
        }
        if let Some(ref tei) = self.traffic_endpoint_id {
            ies.push(tei.to_ie());
        }
        if let Some(ref prox) = self.proxying {
            ies.push(prox.to_ie());
        }
        if let Some(ref iface) = self.three_gpp_interface_type {
            ies.push(iface.to_ie());
        }
        if let Some(ref he) = self.header_enrichment {
            ies.push(he.to_ie());
        }
        if let Some(ref rtfp) = self.redundant_transmission_forwarding_parameters {
            ies.push(rtfp.clone());
        }

        marshal_ies(&ies)
    }

    /// Unmarshals a byte slice into a Forwarding Parameters IE.
    pub fn unmarshal(payload: &[u8]) -> Result<Self, PfcpError> {
        let mut destination_interface = None;
        let mut network_instance = None;
        let mut transport_level_marking = None;
        let mut outer_header_creation = None;
        let mut traffic_endpoint_id = None;
        let mut proxying = None;
        let mut three_gpp_interface_type = None;
        let mut header_enrichment = None;
        let mut redundant_transmission_forwarding_parameters = None;

        for ie_result in IeIterator::new(payload) {
            let ie = ie_result?;
            match ie.ie_type {
                IeType::DestinationInterface => {
                    destination_interface = Some(DestinationInterface::unmarshal(&ie.payload)?)
                }
                IeType::NetworkInstance => {
                    network_instance = Some(NetworkInstance::unmarshal(&ie.payload)?)
                }
                IeType::TransportLevelMarking => {
                    transport_level_marking = Some(TransportLevelMarking::unmarshal(&ie.payload)?)
                }
                IeType::OuterHeaderCreation => {
                    outer_header_creation = Some(OuterHeaderCreation::unmarshal(&ie.payload)?)
                }
                IeType::TrafficEndpointId => {
                    traffic_endpoint_id = Some(TrafficEndpointId::unmarshal(&ie.payload)?)
                }
                IeType::Proxying => proxying = Some(Proxying::unmarshal(&ie.payload)?),
                IeType::TgppInterfaceType => {
                    three_gpp_interface_type =
                        Some(ThreeGppInterfaceTypeIe::unmarshal(&ie.payload)?)
                }
                IeType::HeaderEnrichment => {
                    header_enrichment = Some(HeaderEnrichment::unmarshal(&ie.payload)?)
                }
                IeType::RedundantTransmissionForwardingParameters => {
                    redundant_transmission_forwarding_parameters = Some(ie);
                }
                _ => (),
            }
        }

        let destination_interface = destination_interface.ok_or_else(|| {
            PfcpError::missing_ie_in_grouped(
                IeType::DestinationInterface,
                IeType::ForwardingParameters,
            )
        })?;

        Ok(ForwardingParameters {
            destination_interface,
            network_instance,
            transport_level_marking,
            outer_header_creation,
            traffic_endpoint_id,
            proxying,
            three_gpp_interface_type,
            header_enrichment,
            redundant_transmission_forwarding_parameters,
        })
    }

    /// Wraps the Forwarding Parameters in a ForwardingParameters IE.
    pub fn to_ie(&self) -> Ie {
        Ie::new(IeType::ForwardingParameters, self.marshal())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ie::{
        create_traffic_endpoint::TrafficEndpointId,
        destination_interface::Interface,
        header_enrichment::HeaderEnrichment,
        outer_header_creation::OuterHeaderCreation,
        proxying::Proxying,
        three_gpp_interface_type::{ThreeGppInterfaceType, ThreeGppInterfaceTypeIe},
    };
    use crate::types::Teid;
    use std::net::Ipv4Addr;

    #[test]
    fn test_forwarding_parameters_basic() {
        let params = ForwardingParameters::new(DestinationInterface::new(Interface::Core));

        assert_eq!(params.destination_interface.interface, Interface::Core);
        assert!(params.network_instance.is_none());
        assert!(params.transport_level_marking.is_none());

        let marshaled = params.marshal();
        let unmarshaled = ForwardingParameters::unmarshal(&marshaled).unwrap();

        assert_eq!(params, unmarshaled);
    }

    #[test]
    fn test_forwarding_parameters_with_network_instance() {
        let params = ForwardingParameters::new(DestinationInterface::new(Interface::Access))
            .with_network_instance(NetworkInstance::new("internet.apn"));

        assert!(params.network_instance.is_some());
        assert_eq!(
            params.network_instance.as_ref().unwrap().instance,
            "internet.apn"
        );

        let marshaled = params.marshal();
        let unmarshaled = ForwardingParameters::unmarshal(&marshaled).unwrap();

        assert_eq!(params, unmarshaled);
    }

    #[test]
    fn test_forwarding_parameters_with_outer_header_creation() {
        let outer_header =
            OuterHeaderCreation::gtpu_ipv4(0x12345678, "192.168.1.1".parse().unwrap());

        let params = ForwardingParameters::new(DestinationInterface::new(Interface::Core))
            .with_outer_header_creation(outer_header);

        assert!(params.outer_header_creation.is_some());
        assert_eq!(
            params.outer_header_creation.as_ref().unwrap().teid,
            Some(Teid(0x12345678))
        );

        let marshaled = params.marshal();
        let unmarshaled = ForwardingParameters::unmarshal(&marshaled).unwrap();

        assert_eq!(params, unmarshaled);
    }

    #[test]
    fn test_forwarding_parameters_with_traffic_endpoint_id() {
        let traffic_endpoint_id = TrafficEndpointId::new(42);

        let params = ForwardingParameters::new(DestinationInterface::new(Interface::Access))
            .with_traffic_endpoint_id(traffic_endpoint_id);

        assert!(params.traffic_endpoint_id.is_some());
        assert_eq!(params.traffic_endpoint_id.as_ref().unwrap().id, 42);

        let marshaled = params.marshal();
        let unmarshaled = ForwardingParameters::unmarshal(&marshaled).unwrap();

        assert_eq!(params, unmarshaled);
    }

    #[test]
    fn test_forwarding_parameters_with_proxying() {
        let proxying = Proxying::both();

        let params = ForwardingParameters::new(DestinationInterface::new(Interface::Core))
            .with_proxying(proxying);

        assert!(params.proxying.is_some());
        assert!(params.proxying.as_ref().unwrap().arp);
        assert!(params.proxying.as_ref().unwrap().inp);

        let marshaled = params.marshal();
        let unmarshaled = ForwardingParameters::unmarshal(&marshaled).unwrap();

        assert_eq!(params, unmarshaled);
    }

    #[test]
    fn test_forwarding_parameters_with_three_gpp_interface_type() {
        let interface_type = ThreeGppInterfaceTypeIe::new(ThreeGppInterfaceType::N6);

        let params = ForwardingParameters::new(DestinationInterface::new(Interface::Core))
            .with_three_gpp_interface_type(interface_type);

        assert!(params.three_gpp_interface_type.is_some());
        assert_eq!(
            params
                .three_gpp_interface_type
                .as_ref()
                .unwrap()
                .interface_type,
            ThreeGppInterfaceType::N6
        );

        let marshaled = params.marshal();
        let unmarshaled = ForwardingParameters::unmarshal(&marshaled).unwrap();

        assert_eq!(params, unmarshaled);
    }

    #[test]
    fn test_forwarding_parameters_with_header_enrichment() {
        let header_enrichment =
            HeaderEnrichment::http_header("X-Operator-ID".to_string(), "operator123".to_string());

        let params = ForwardingParameters::new(DestinationInterface::new(Interface::Access))
            .with_header_enrichment(header_enrichment);

        assert!(params.header_enrichment.is_some());
        assert_eq!(
            params.header_enrichment.as_ref().unwrap().name,
            "X-Operator-ID"
        );

        let marshaled = params.marshal();
        let unmarshaled = ForwardingParameters::unmarshal(&marshaled).unwrap();

        assert_eq!(params, unmarshaled);
    }

    #[test]
    fn test_forwarding_parameters_comprehensive() {
        // Test with all optional parameters
        let params = ForwardingParameters::new(DestinationInterface::new(Interface::Core))
            .with_network_instance(NetworkInstance::new("internet.apn"))
            .with_transport_level_marking(TransportLevelMarking::new(0x2B))
            .with_outer_header_creation(OuterHeaderCreation::gtpu_ipv4(
                0x12345678,
                "192.168.1.1".parse().unwrap(),
            ))
            .with_traffic_endpoint_id(TrafficEndpointId::new(42))
            .with_proxying(Proxying::both())
            .with_three_gpp_interface_type(ThreeGppInterfaceTypeIe::new(ThreeGppInterfaceType::N6))
            .with_header_enrichment(HeaderEnrichment::http_header(
                "X-Custom".to_string(),
                "value".to_string(),
            ));

        // Verify all fields are set
        assert!(params.network_instance.is_some());
        assert!(params.transport_level_marking.is_some());
        assert!(params.outer_header_creation.is_some());
        assert!(params.traffic_endpoint_id.is_some());
        assert!(params.proxying.is_some());
        assert!(params.three_gpp_interface_type.is_some());
        assert!(params.header_enrichment.is_some());

        // Test round-trip marshal/unmarshal
        let marshaled = params.marshal();
        let unmarshaled = ForwardingParameters::unmarshal(&marshaled).unwrap();

        assert_eq!(params, unmarshaled);
    }

    #[test]
    fn test_forwarding_parameters_to_ie() {
        let params = ForwardingParameters::new(DestinationInterface::new(Interface::Core))
            .with_network_instance(NetworkInstance::new("test.apn"));

        let ie = params.to_ie();

        assert_eq!(ie.ie_type, IeType::ForwardingParameters);

        let unmarshaled = ForwardingParameters::unmarshal(&ie.payload).unwrap();
        assert_eq!(params, unmarshaled);
    }

    #[test]
    fn test_forwarding_parameters_5g_uplink_scenario() {
        // Typical 5G uplink scenario: Access -> Core with GTP-U encapsulation
        let params = ForwardingParameters::new(DestinationInterface::new(Interface::Core))
            .with_outer_header_creation(OuterHeaderCreation::gtpu_ipv4(
                0xABCDEF01,
                Ipv4Addr::new(10, 0, 1, 100),
            ))
            .with_three_gpp_interface_type(ThreeGppInterfaceTypeIe::new(ThreeGppInterfaceType::N3));

        let marshaled = params.marshal();
        let unmarshaled = ForwardingParameters::unmarshal(&marshaled).unwrap();

        assert_eq!(params, unmarshaled);
        assert!(unmarshaled
            .three_gpp_interface_type
            .unwrap()
            .interface_type
            .is_5g_interface());
    }

    #[test]
    fn test_forwarding_parameters_5g_downlink_scenario() {
        // Typical 5G downlink scenario: Core -> Access with proxying
        let params = ForwardingParameters::new(DestinationInterface::new(Interface::Access))
            .with_network_instance(NetworkInstance::new("ims.apn"))
            .with_proxying(Proxying::arp())
            .with_three_gpp_interface_type(ThreeGppInterfaceTypeIe::new(ThreeGppInterfaceType::N6));

        let marshaled = params.marshal();
        let unmarshaled = ForwardingParameters::unmarshal(&marshaled).unwrap();

        assert_eq!(params, unmarshaled);
    }
}