rs-pfcp 0.1.2

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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# PFCP Examples Guide

This comprehensive guide walks you through all the working examples in the rs-pfcp library, from simple heartbeat mechanisms to complex session management scenarios.

## 📋 Quick Reference

| Example | Purpose | Complexity | Use Case |
|---------|---------|------------|----------|
| **[Heartbeat Client/Server]#heartbeat-examples** | Node connectivity testing | 🟢 Basic | Keep-alive mechanism |
| **[Session Client/Server]#session-management-examples** | Complete session lifecycle | 🟡 Intermediate | SMF ↔ UPF communication |
| **[PCAP Reader]#traffic-analysis-example** | Protocol traffic analysis | 🟡 Intermediate | Network debugging |
| **[Session Report Demo]#session-reporting-demo** | Usage reporting workflow | 🔴 Advanced | Quota management |
| **[PDN Type Examples]#pdn-type-examples** | Data network configuration | 🟢 Basic | Connection type handling |
| **[Header Length Test]#header-length-test** | Protocol compliance testing | 🟢 Basic | Development/testing |

## 🚀 Getting Started

### Prerequisites

```bash
# Ensure you have Rust installed
rustc --version

# Clone and build the project
git clone <repository-url>
cd rs-pfcp
cargo build

# Test that examples compile
cargo check --examples
```

### Network Setup

Most examples require network interface access:

```bash
# Check available interfaces
ip link show

# Common interfaces:
# - lo (loopback) - for local testing
# - eth0, ens33, wlan0 - for real network testing
```

---

## 💓 Heartbeat Examples

**Purpose**: Demonstrate basic PFCP node connectivity and keep-alive mechanisms

### Heartbeat Server

**Location**: `examples/heartbeat-server/main.rs`

**What it does**:
- Listens for PFCP heartbeat requests
- Responds with heartbeat responses containing recovery timestamps
- Simulates a UPF responding to SMF health checks

**Usage**:
```bash
# Start server on localhost:8805
cargo run --example heartbeat-server

# Start server on specific interface and port
cargo run --example heartbeat-server -- --interface eth0 --port 8806
```

**Key Features**:
- Multi-interface support with automatic IP detection
- Recovery timestamp management
- Graceful error handling

### Heartbeat Client

**Location**: `examples/heartbeat-client/main.rs`

**What it does**:
- Sends heartbeat requests to a PFCP server
- Includes recovery timestamps and source IP information
- Demonstrates basic UDP socket communication

**Usage**:
```bash
# Send heartbeat to default server (127.0.0.1:8805)
cargo run --example heartbeat-client

# In separate terminals:
Terminal 1: cargo run --example heartbeat-server
Terminal 2: cargo run --example heartbeat-client
```

**Expected Output**:
```
Server Output:
Listening on 192.168.1.100:8805...
Received Heartbeat Request from 127.0.0.1:xxxxx
  Recovery Timestamp: 2024-01-15 10:30:45 UTC
  Source IP: 127.0.0.1 / 2001::1

Client Output:
Heartbeat Request sent to 127.0.0.1:8805
Received Heartbeat Response:
  Recovery Timestamp: 2024-01-15 10:30:46 UTC
```

**Learning Objectives**:
- Basic PFCP message construction
- UDP socket communication
- Recovery timestamp handling
- IPv4/IPv6 dual-stack support

---

## 🔄 Session Management Examples

**Purpose**: Complete PFCP session lifecycle including establishment, modification, and deletion

### Session Server (UPF Simulator)

**Location**: `examples/session-server/main.rs`

**What it does**:
- Simulates a User Plane Function (UPF)
- Handles association setup/release requests
- Manages PFCP session establishment, modification, and deletion
- Simulates quota exhaustion and sends usage reports

**Usage**:
```bash
# Start on loopback interface
cargo run --example session-server -- --interface lo --port 8805

# Start on specific network interface
cargo run --example session-server -- --interface eth0 --port 8806
```

**Key Capabilities**:
- **Association Management**: Establishes PFCP associations with SMFs
- **Session Lifecycle**: Handles complete session establishment/modification/deletion
- **Usage Reporting**: Simulates quota exhaustion after 2 seconds
- **Multi-Session Support**: Handles multiple concurrent sessions

### Session Client (SMF Simulator)

**Location**: `examples/session-client/main.rs`

**What it does**:
- Simulates a Session Management Function (SMF)
- Establishes associations with UPFs
- Creates, modifies, and deletes PFCP sessions
- Handles incoming usage reports

**Usage**:
```bash
# Create single session with default server
cargo run --example session-client -- --sessions 1

# Create multiple sessions
cargo run --example session-client -- --sessions 5 --address 192.168.1.100 --port 8805

# Use specific interface
cargo run --example session-client -- --interface eth0 --sessions 3
```

**Command Line Options**:
- `--sessions N`: Number of sessions to create (default: 1)
- `--interface IFACE`: Network interface to use (default: lo)
- `--address ADDR`: Server IP address (default: 127.0.0.1)
- `--port PORT`: Server port (default: 8805)

**Session Flow Demonstration**:
```
1. Association Setup Request/Response
2. Session Establishment Request/Response (with PDR/FAR creation)
3. [Server simulates quota exhaustion after 2s]
4. Session Report Request (from server) → Response (from client)
5. Session Deletion Request/Response
6. Association Release Request/Response
```

**Expected Output**:
```bash
Client Output:
Sending Association Setup Request...
Received Association Setup Response.

--- Starting Session 1 ---
[1] Sending Session Establishment Request...
[1] Received Session Establishment Response.
[1] Listening for Session Report Requests...
  Received Session Report Request
    Report Type: Usage Report (USAR)
    Contains Usage Report - quota exhausted!
  Sent Session Report Response (RequestAccepted)
[1] Sending Session Deletion Request...
[1] Received Session Deletion Response.

Server Output:
Listening on 127.0.0.1:8805...
Received AssociationSetupRequest from 127.0.0.1:xxxxx
Received SessionEstablishmentRequest from 127.0.0.1:xxxxx
  Session ID: 0x0000000000000001
  [QUOTA EXHAUSTED] Sending Session Report Request for session 0x0000000000000001
  Received Session Report Response - quota exhaustion acknowledged
```

---

## 📊 Traffic Analysis Example

### PCAP Reader

**Location**: `examples/pcap-reader/main.rs`

**Purpose**: Analyze captured PFCP traffic and convert binary protocol data to human-readable format

**What it does**:
- Reads PCAP files containing network traffic
- Filters for PFCP messages (UDP port 8805)
- Parses binary PFCP data into structured messages
- Outputs messages in YAML or JSON format for analysis

**Usage**:
```bash
# Analyze all traffic in PCAP file
cargo run --example pcap-reader -- --pcap traffic.pcap

# Show only PFCP messages in YAML format
cargo run --example pcap-reader -- --pcap traffic.pcap --pfcp-only --format yaml

# Output JSON for programmatic analysis
cargo run --example pcap-reader -- --pcap traffic.pcap --pfcp-only --format json
```

**Command Line Options**:
- `--pcap FILE`: Path to PCAP file (required)
- `--pfcp-only`: Filter to show only PFCP messages
- `--format FORMAT`: Output format (yaml/json, default: yaml)

**Example Output**:
```yaml
Packet 1:
  timestamp: 2024-01-15T10:30:45.123Z
  src: 192.168.1.10:45678
  dst: 192.168.1.100:8805
  message_type: SessionEstablishmentRequest
  sequence_number: 42
  session_id: 0x123456789abcdef0
  information_elements:
    - type: NodeId
      value: "192.168.1.10"
    - type: Fseid
      value:
        session_id: 0x123456789abcdef0
        ipv4_address: "192.168.1.10"
    - type: CreatePdr
      value:
        pdr_id: 1
        precedence: 100
        pdi: {...}
```

**Use Cases**:
- **Network Debugging**: Analyze failed PFCP communications
- **Protocol Compliance**: Verify message format correctness
- **Performance Analysis**: Identify bottlenecks and patterns
- **Development Testing**: Validate new implementations

---

## 📈 Session Reporting Demo

**Purpose**: Complete end-to-end demonstration of quota management and usage reporting

### The Complete Demo

**Files**:
- Script: `examples/test_session_report.sh`
- Documentation: `examples/SESSION_REPORT_DEMO.md`
- Server: `examples/session-server/main.rs`
- Client: `examples/session-client/main.rs`

**What it demonstrates**:
- Complete session establishment
- Quota exhaustion simulation
- Usage report generation (UPF → SMF)
- Report acknowledgment (SMF → UPF)
- Real-time packet capture and analysis

**Usage**:
```bash
# Run complete demo with packet capture
cd examples
./test_session_report.sh

# Use specific interface
./test_session_report.sh eth0

# Manual step-by-step execution:
# Terminal 1: Start server
cargo run --example session-server -- --interface lo --port 8805

# Terminal 2: Start packet capture (optional)
tcpdump -i lo -w session_demo.pcap udp

# Terminal 3: Run client
cargo run --example session-client -- --sessions 1

# Terminal 4: Analyze captured traffic
cargo run --example pcap-reader -- --pcap session_demo.pcap --pfcp-only
```

**Demo Flow**:
1. **Setup**: Start packet capture and server
2. **Association**: Client establishes PFCP association
3. **Session Creation**: Client creates session with PDR/FAR rules
4. **Quota Simulation**: Server simulates data usage and quota exhaustion
5. **Usage Reporting**: Server sends Session Report Request with volume threshold trigger
6. **Acknowledgment**: Client responds with Session Report Response
7. **Analysis**: Captured packets are analyzed and displayed

**Expected Files Generated**:
- `session_report_TIMESTAMP.pcap`: Raw packet capture
- `session_report_TIMESTAMP_analysis.yaml`: Parsed PFCP messages

**Real-World Application**:
This demonstrates the critical 5G network flow where:
- UPF monitors user data consumption against quotas
- UPF reports quota exhaustion to SMF
- SMF can grant additional quota, apply policies, or terminate sessions
- All communication is captured for audit and troubleshooting

---

## 🌐 PDN Type Examples

**Purpose**: Demonstrate PDN (Packet Data Network) connection type handling

### PDN Type Simple

**Location**: `examples/pdn-type-simple.rs`

**What it does**:
- Shows basic PDN Type IE creation and marshaling
- Demonstrates different connection types (IPv4, IPv6, IPv4v6, Non-IP, Ethernet)

**Usage**:
```bash
cargo run --example pdn-type-simple
```

**Example Output**:
```
PDN Type Examples:
  IPv4: [0x01]
  IPv6: [0x02]
  IPv4v6: [0x03]
  Non-IP: [0x04]
  Ethernet: [0x05]
```

### PDN Type Demo

**Location**: `examples/pdn-type-demo.rs`

**What it does**:
- Advanced PDN Type usage in session establishment
- Shows integration with other IEs in realistic scenarios

**Usage**:
```bash
cargo run --example pdn-type-demo
```

**Learning Objectives**:
- Understanding 5G connection types
- PDN Type IE construction and usage
- Integration with session establishment

---

## 🔍 Header Length Test

**Location**: `examples/header-length-test/main.rs`

**Purpose**: Validate PFCP header length calculations and protocol compliance

**What it does**:
- Tests header length calculations for different message types
- Validates SEID presence/absence handling
- Ensures protocol compliance with 3GPP TS 29.244

**Usage**:
```bash
cargo run --example header-length-test
```

**Use Cases**:
- Development testing
- Protocol compliance verification
- Regression testing

---

## 🛠️ Development Workflows

### Complete Testing Workflow

```bash
# 1. Build all examples
cargo build --examples

# 2. Test basic connectivity
cargo run --example heartbeat-server &
SERVER_PID=$!
sleep 1
cargo run --example heartbeat-client
kill $SERVER_PID

# 3. Test session management
cargo run --example session-server -- --interface lo &
SERVER_PID=$!
sleep 2
cargo run --example session-client -- --sessions 3
kill $SERVER_PID

# 4. Run comprehensive demo
cd examples
./test_session_report.sh lo

# 5. Analyze generated traffic
cargo run --example pcap-reader -- --pcap session_report_*.pcap --pfcp-only
```

### Network Debugging Workflow

```bash
# 1. Start packet capture
tcpdump -i any -w pfcp_debug.pcap 'udp port 8805' &
TCPDUMP_PID=$!

# 2. Run your PFCP application
your-pfcp-application

# 3. Stop capture
kill $TCPDUMP_PID

# 4. Analyze with rs-pfcp
cargo run --example pcap-reader -- --pcap pfcp_debug.pcap --pfcp-only --format json > analysis.json

# 5. Review results
cat analysis.json | jq '.[] | select(.message_type == "SessionEstablishmentRequest")'
```

### Performance Testing

```bash
# Test with multiple sessions
time cargo run --example session-client -- --sessions 100

# Stress test server
for i in {1..10}; do
    cargo run --example session-client -- --sessions 50 &
done
wait

# Analyze performance
cargo run --example pcap-reader -- --pcap stress_test.pcap --pfcp-only | grep -c "message_type"
```

## 🚨 Troubleshooting

### Common Issues

#### "Address already in use"
```bash
# Check what's using port 8805
lsof -i :8805
netstat -tulpn | grep 8805

# Kill existing processes
pkill -f session-server
```

#### "Permission denied" for network interfaces
```bash
# Run with appropriate permissions
sudo cargo run --example session-server -- --interface eth0

# Or use user-accessible interfaces
cargo run --example session-server -- --interface lo
```

#### "No such device" for network interface
```bash
# List available interfaces
ip link show
ifconfig -a

# Use existing interface
cargo run --example session-server -- --interface lo
```

#### PCAP file empty or not created
```bash
# Check tcpdump permissions
sudo tcpdump --version

# Use alternative capture methods
tshark -i lo -w capture.pcap -f 'udp port 8805'
```

### Debug Output

Enable verbose logging:
```bash
# Set Rust logging level
RUST_LOG=debug cargo run --example session-client -- --sessions 1

# Enable all logs
RUST_LOG=trace cargo run --example session-server
```

### Message Analysis

```bash
# Get detailed message breakdown
cargo run --example pcap-reader -- --pcap file.pcap --format json | jq '.[0].information_elements'

# Count message types
cargo run --example pcap-reader -- --pcap file.pcap --format json | jq '.[].message_type' | sort | uniq -c

# Find specific sessions
cargo run --example pcap-reader -- --pcap file.pcap --format json | jq '.[] | select(.session_id == "0x123456789abcdef0")'
```

---

## 🎯 Next Steps

After completing these examples:

1. **Study the Code**: Examine example source code to understand implementation patterns
2. **Modify Examples**: Adapt examples for your specific use case
3. **Read Documentation**:
   - [API_GUIDE.md]API_GUIDE.md for detailed API usage
   - [PFCP_MESSAGES.md]PFCP_MESSAGES.md for message specifications
   - [IE_SUPPORT.md]IE_SUPPORT.md for Information Element details
4. **Build Your Application**: Use examples as templates for production code

## 📚 Additional Resources

- **3GPP TS 29.244**: Official PFCP specification
- **5G Core Network Architecture**: Understanding SMF and UPF roles
- **Wireshark PFCP Dissector**: For advanced traffic analysis
- **Network Simulation Tools**: mininet, GNS3 for testing environments

---

**Ready to build production 5G networks?** These examples provide the foundation for robust PFCP implementations! 🚀