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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Type-safe newtype wrappers for PFCP protocol identifiers.
//!
//! This module provides newtype wrappers that prevent accidental argument swapping
//! at compile time. For example, mixing up `seid` and `sequence_number` parameters
//! is a common source of bugs that these types eliminate.
//!
//! # Types
//!
//! - [`Seid`] - Session Endpoint Identifier (64-bit)
//! - [`SequenceNumber`] - Message sequence number (24-bit, stored as u32)
//! - [`Teid`] - Tunnel Endpoint Identifier (32-bit)
//!
//! # Examples
//!
//! ```rust
//! use rs_pfcp::types::{Seid, SequenceNumber, Teid};
//!
//! // Create from primitives
//! let seid = Seid(0x123456789ABCDEF0);
//! let seq = SequenceNumber::new(42);
//! let teid = Teid(0x12345678);
//!
//! // Convert back to primitives
//! let seid_val: u64 = seid.into();
//! let seq_val: u32 = seq.into();
//! let teid_val: u32 = teid.into();
//!
//! // Use From trait
//! let seid2: Seid = 100u64.into();
//! let seq2: SequenceNumber = 50u32.into();
//! let teid2: Teid = 200u32.into();
//!
//! // Access inner value via Deref
//! assert_eq!(*seid, 0x123456789ABCDEF0u64);
//! ```

use std::fmt;
use std::ops::Deref;

// ============================================================================
// Seid - Session Endpoint Identifier
// ============================================================================

/// Session Endpoint Identifier (SEID) - 64-bit identifier.
///
/// The SEID uniquely identifies a PFCP session on a given node.
/// Per 3GPP TS 29.244, the SEID is present in session-related messages
/// to identify the PFCP session context.
///
/// # Examples
///
/// ```rust
/// use rs_pfcp::types::Seid;
///
/// let seid = Seid(0x123456789ABCDEF0);
/// assert_eq!(*seid, 0x123456789ABCDEF0);
///
/// // From/Into conversions
/// let seid: Seid = 42u64.into();
/// let value: u64 = seid.into();
/// assert_eq!(value, 42);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Seid(pub u64);

impl Seid {
    /// Creates a new SEID with the given value.
    #[inline]
    pub const fn new(value: u64) -> Self {
        Self(value)
    }

    /// Returns the inner u64 value.
    #[inline]
    pub const fn value(&self) -> u64 {
        self.0
    }
}

impl From<u64> for Seid {
    #[inline]
    fn from(value: u64) -> Self {
        Self(value)
    }
}

impl From<Seid> for u64 {
    #[inline]
    fn from(seid: Seid) -> Self {
        seid.0
    }
}

impl Deref for Seid {
    type Target = u64;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl fmt::Display for Seid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "0x{:016X}", self.0)
    }
}

impl fmt::LowerHex for Seid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::LowerHex::fmt(&self.0, f)
    }
}

impl fmt::UpperHex for Seid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::UpperHex::fmt(&self.0, f)
    }
}

// ============================================================================
// SequenceNumber - 24-bit Message Sequence Number
// ============================================================================

/// Sequence Number - 24-bit message sequence number (stored as u32).
///
/// Per 3GPP TS 29.244 Section 5.1, the sequence number is a 24-bit field
/// used to match request and response messages. Valid range: 0 to 0x00FFFFFF.
///
/// # Examples
///
/// ```rust
/// use rs_pfcp::types::SequenceNumber;
///
/// // Create with masking to 24 bits
/// let seq = SequenceNumber::new(0x123456);
/// assert_eq!(*seq, 0x123456);
///
/// // Values larger than 24 bits are masked
/// let seq = SequenceNumber::new(0xFF123456);
/// assert_eq!(*seq, 0x123456);
///
/// // Increment with wrap-around
/// let seq = SequenceNumber::new(SequenceNumber::MAX);
/// let next = seq.next();
/// assert_eq!(*next, 0);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct SequenceNumber(pub u32);

impl SequenceNumber {
    /// Maximum value for a 24-bit sequence number.
    pub const MAX: u32 = 0x00FFFFFF;

    /// Creates a new sequence number, masking to 24 bits.
    #[inline]
    pub const fn new(value: u32) -> Self {
        Self(value & Self::MAX)
    }

    /// Returns the inner u32 value.
    #[inline]
    pub const fn value(&self) -> u32 {
        self.0
    }

    /// Returns the next sequence number with wrap-around at MAX.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rs_pfcp::types::SequenceNumber;
    ///
    /// let seq = SequenceNumber::new(100);
    /// assert_eq!(*seq.next(), 101);
    ///
    /// let max_seq = SequenceNumber::new(SequenceNumber::MAX);
    /// assert_eq!(*max_seq.next(), 0);
    /// ```
    #[inline]
    pub const fn next(&self) -> Self {
        Self((self.0 + 1) & Self::MAX)
    }

    /// Checks if this sequence number is within a valid 24-bit range.
    ///
    /// Always returns true since the constructor masks to 24 bits.
    #[inline]
    pub const fn is_valid(&self) -> bool {
        self.0 <= Self::MAX
    }
}

impl From<u32> for SequenceNumber {
    #[inline]
    fn from(value: u32) -> Self {
        Self::new(value)
    }
}

impl From<SequenceNumber> for u32 {
    #[inline]
    fn from(seq: SequenceNumber) -> Self {
        seq.0
    }
}

impl Deref for SequenceNumber {
    type Target = u32;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl fmt::Display for SequenceNumber {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

// ============================================================================
// Teid - Tunnel Endpoint Identifier
// ============================================================================

/// Tunnel Endpoint Identifier (TEID) - 32-bit identifier.
///
/// The TEID identifies a GTP-U tunnel endpoint. It is used in F-TEID IEs
/// to specify tunnel endpoints for user plane data forwarding.
///
/// # Examples
///
/// ```rust
/// use rs_pfcp::types::Teid;
///
/// let teid = Teid(0x12345678);
/// assert_eq!(*teid, 0x12345678);
///
/// // From/Into conversions
/// let teid: Teid = 42u32.into();
/// let value: u32 = teid.into();
/// assert_eq!(value, 42);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Teid(pub u32);

impl Teid {
    /// Creates a new TEID with the given value.
    #[inline]
    pub const fn new(value: u32) -> Self {
        Self(value)
    }

    /// Returns the inner u32 value.
    #[inline]
    pub const fn value(&self) -> u32 {
        self.0
    }
}

impl From<u32> for Teid {
    #[inline]
    fn from(value: u32) -> Self {
        Self(value)
    }
}

impl From<Teid> for u32 {
    #[inline]
    fn from(teid: Teid) -> Self {
        teid.0
    }
}

impl Deref for Teid {
    type Target = u32;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl fmt::Display for Teid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "0x{:08X}", self.0)
    }
}

impl fmt::LowerHex for Teid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::LowerHex::fmt(&self.0, f)
    }
}

impl fmt::UpperHex for Teid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::UpperHex::fmt(&self.0, f)
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    // Seid tests
    mod seid_tests {
        use super::*;

        #[test]
        fn test_seid_new() {
            let seid = Seid::new(0x123456789ABCDEF0);
            assert_eq!(seid.value(), 0x123456789ABCDEF0);
        }

        #[test]
        fn test_seid_default() {
            let seid = Seid::default();
            assert_eq!(*seid, 0);
        }

        #[test]
        fn test_seid_from_u64() {
            let seid: Seid = 42u64.into();
            assert_eq!(*seid, 42);
        }

        #[test]
        fn test_seid_into_u64() {
            let seid = Seid(100);
            let value: u64 = seid.into();
            assert_eq!(value, 100);
        }

        #[test]
        fn test_seid_deref() {
            let seid = Seid(0xDEADBEEF);
            assert_eq!(*seid, 0xDEADBEEF);
        }

        #[test]
        fn test_seid_display() {
            let seid = Seid(0x123456789ABCDEF0);
            assert_eq!(format!("{}", seid), "0x123456789ABCDEF0");
        }

        #[test]
        fn test_seid_equality() {
            let seid1 = Seid(100);
            let seid2 = Seid(100);
            let seid3 = Seid(200);
            assert_eq!(seid1, seid2);
            assert_ne!(seid1, seid3);
        }

        #[test]
        fn test_seid_clone() {
            let seid = Seid(42);
            let cloned = seid;
            assert_eq!(seid, cloned);
        }

        #[test]
        fn test_seid_hash() {
            use std::collections::HashSet;
            let mut set = HashSet::new();
            set.insert(Seid(1));
            set.insert(Seid(2));
            set.insert(Seid(1));
            assert_eq!(set.len(), 2);
        }
    }

    // SequenceNumber tests
    mod sequence_number_tests {
        use super::*;

        #[test]
        fn test_sequence_number_new() {
            let seq = SequenceNumber::new(42);
            assert_eq!(seq.value(), 42);
        }

        #[test]
        fn test_sequence_number_max() {
            assert_eq!(SequenceNumber::MAX, 0x00FFFFFF);
        }

        #[test]
        fn test_sequence_number_masking() {
            let seq = SequenceNumber::new(0xFF123456);
            assert_eq!(*seq, 0x123456);
        }

        #[test]
        fn test_sequence_number_default() {
            let seq = SequenceNumber::default();
            assert_eq!(*seq, 0);
        }

        #[test]
        fn test_sequence_number_next() {
            let seq = SequenceNumber::new(100);
            assert_eq!(*seq.next(), 101);
        }

        #[test]
        fn test_sequence_number_next_wrap() {
            let seq = SequenceNumber::new(SequenceNumber::MAX);
            assert_eq!(*seq.next(), 0);
        }

        #[test]
        fn test_sequence_number_from_u32() {
            let seq: SequenceNumber = 50u32.into();
            assert_eq!(*seq, 50);
        }

        #[test]
        fn test_sequence_number_into_u32() {
            let seq = SequenceNumber::new(75);
            let value: u32 = seq.into();
            assert_eq!(value, 75);
        }

        #[test]
        fn test_sequence_number_deref() {
            let seq = SequenceNumber::new(999);
            assert_eq!(*seq, 999);
        }

        #[test]
        fn test_sequence_number_display() {
            let seq = SequenceNumber::new(12345);
            assert_eq!(format!("{}", seq), "12345");
        }

        #[test]
        fn test_sequence_number_ordering() {
            let seq1 = SequenceNumber::new(10);
            let seq2 = SequenceNumber::new(20);
            assert!(seq1 < seq2);
            assert!(seq2 > seq1);
        }

        #[test]
        fn test_sequence_number_is_valid() {
            let seq = SequenceNumber::new(100);
            assert!(seq.is_valid());

            let seq_max = SequenceNumber::new(SequenceNumber::MAX);
            assert!(seq_max.is_valid());
        }
    }

    // Teid tests
    mod teid_tests {
        use super::*;

        #[test]
        fn test_teid_new() {
            let teid = Teid::new(0x12345678);
            assert_eq!(teid.value(), 0x12345678);
        }

        #[test]
        fn test_teid_default() {
            let teid = Teid::default();
            assert_eq!(*teid, 0);
        }

        #[test]
        fn test_teid_from_u32() {
            let teid: Teid = 42u32.into();
            assert_eq!(*teid, 42);
        }

        #[test]
        fn test_teid_into_u32() {
            let teid = Teid(100);
            let value: u32 = teid.into();
            assert_eq!(value, 100);
        }

        #[test]
        fn test_teid_deref() {
            let teid = Teid(0xDEADBEEF);
            assert_eq!(*teid, 0xDEADBEEF);
        }

        #[test]
        fn test_teid_display() {
            let teid = Teid(0x12345678);
            assert_eq!(format!("{}", teid), "0x12345678");
        }

        #[test]
        fn test_teid_equality() {
            let teid1 = Teid(100);
            let teid2 = Teid(100);
            let teid3 = Teid(200);
            assert_eq!(teid1, teid2);
            assert_ne!(teid1, teid3);
        }

        #[test]
        fn test_teid_clone() {
            let teid = Teid(42);
            let cloned = teid;
            assert_eq!(teid, cloned);
        }

        #[test]
        fn test_teid_hash() {
            use std::collections::HashSet;
            let mut set = HashSet::new();
            set.insert(Teid(1));
            set.insert(Teid(2));
            set.insert(Teid(1));
            assert_eq!(set.len(), 2);
        }
    }
}