> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/upg/3.2/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/upg/3.2/deployment-guides/tdf.md).

# Traffic Detection Function

UPG provides both [Traffic Detection Function (TDF)](https://docs.cennso.com/components/upg/3.2/use-cases/tdf.md) and [Carrier-grade NAT (CG-NAT)](https://docs.cennso.com/components/upg/3.2/operation-guides/cg-nat.md) capabilities. These are commonly combined into a single deployment to simplify network architecture and reduce operational overhead.

To deploy UPG as a Traffic Detection Function (TDF), you must deploy two Custom Resources (CRs) to your cluster:
1. [Deploy FPPNetworks](#deploy-fppnetworks)
1. [Deploy `FPPNetworks`](#deploy-fppnetworks)
2. [Deploy `UPGDeployment`](#deploy-upgdeployment)

Read the sections below to learn how to configure these CRs.

## Deploy FPPNetworks

In the Core Cluster, create FPPNetworks for the Access and Control network interfaces (`access-net` and `control-net` respectively), independent of the `UPGDeployment` CR. Additionally, create networks per NAT pool (`at-net` and `de-net`) to separate the NATed traffic. This enables cross-node connectivity from the access network CGW and CGWs on the data network side. For example:

```yaml
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: FPPNetwork
metadata:
  name: access-net
spec:
  type: vxlan
  id: 4201
---
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: FPPNetwork
metadata:
  name: control-net
spec:
  type: vxlan
  id: 4101
---
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: FPPNetwork
metadata:
  name: at-net
spec:
  type: vxlan
  id: 4302
---
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: FPPNetwork
metadata:
  name: de-net
spec:
  type: vxlan
  id: 4303
```

## Deploy UPGDeployment

Create a [UPGDeployment](https://docs.cennso.com/components/upg/3.2/concepts/upgdeployment.md) Custom Resource (CR) in the Management Cluster. For example:

```yaml
apiVersion: fpp.cennso.com/v1alpha1
kind: UPGDeployment
metadata:
  name: upg-tdf
  namespace: cennso-infra
spec:
  version: <version>
  connectionSecretRef:
    name: <secret-name>
    namespace: <secret-namespace>
  repository: oci://registry.cennso.com/cennso/charts
  targetNamespace: tdf-example
  operatorName: upg-operator

  # TDF uses tdf type
  type: tdf

  labels:
    apn: test.apn.cennso

  imagePullSecrets:
    - name: image-secret

  # According to present KPI assumptions: in total 8 GB available
  heapsize: "7G"

  enableHugepagesAllocation: false
  # According to present KPI assumptions
  resources:
    cpu: "2"
    memory: "8Gi"
    hugepages-2Mi: "2Gi"

  nat:
    enabled: true
    type: cgnat
    minNatPort: 1024
    maxNatPort: 65535

  instances:
    inst01:
      networking:
        grx:
          interface: an1
          networkName: access-net
          ipv4:
            addresses:
              - 198.18.1.21/24
            routes:
              # UE IP range
              - to: 10.0.0.0/10
                via: 198.18.1.100
        sx:
          interface: sx1
          networkName: control-net
          mtu: 8930
          ipv4:
            addresses:
              -  172.18.1.21/24
      nat:
        pools:
          pool-de:
            networking:
              interface: de1
              networkName: de-net
              mtu: 1500
              ipv4:
                gw: 198.19.2.1
                addresses:
                  - 198.19.2.21/24
            start: 198.19.8.1
            end: 198.19.8.254
            blocksize: 512
          pool-at:
            networking:
              interface: at1
              networkName: at-net
              mtu: 1500
              ipv4:
                gw: 198.19.3.1
                addresses:
                  - 198.19.3.21/24
            start: 198.19.16.1
            end: 198.19.16.254
            blocksize: 512
    inst02:
      networking:
        grx:
          interface: an1
          networkName: access-net
          ipv4:
            addresses:
              - 198.18.1.22/24
            routes:
              - to: 10.0.0.0/10
                via: 198.18.1.100
        sx:
          interface: sx1
          networkName: control-net
          mtu: 8930
          ipv4:
            addresses:
              -  172.18.1.22/24
      nat:
        pools:
          pool-de:
            networking:
              interface: de1
              networkName: de-net
              mtu: 1500
              ipv4:
                gw: 198.19.2.1
                addresses:
                  - 198.19.2.22/24
            start: 198.19.9.1
            end: 198.19.9.254
            blocksize: 64
          pool-at:
            networking:
              interface: at1
              networkName: at-net
              mtu: 1500
              ipv4:
                gw: 198.19.3.1
                addresses:
                  - 198.19.3.22/24
            start: 198.19.17.1
            end: 198.19.17.254
            blocksize: 64
```

1. Set the **spec.operatorName** to define the FPP Operator which should handle the UPG deployment creation:

   ```yaml
   spec:
     operatorName: upg-operator
   ```

2. Set the **spec.type** field to `tdf` to specify the use case in which UPG should run. By default, UPG operates in `pgw` mode, which configures GTP-U tunnels handling on the access network side. For TDF, `tdf` mode is used:

   ```yaml
   spec:
     type: tdf
   ```

3. Define labels used by the Grafana dashboard and the observability subsystem provided by the `fpp-support` chart:

   ```yaml
   spec:
     labels:
       apn: test.apn.cennso
   ```

4. If using a private registry, add **spec.imagePullSecrets**:

   ```yaml
   spec:
     imagePullSecrets:
       - name: image-secret
   ```

   This sets the service account and the UPG deployment to use the secret. You must create the secret outside of the UPG deployment.

5. Allocate sufficient resources for UPG based on the expected load for 250k sessions:

   ```yaml
   spec:
     heapsize: "7G"
     enableHugepagesAllocation: false
     resources:
       cpu: "2"
       memory: "8Gi"
       hugepages-2Mi: "2Gi"
   ```

   The **spec.enableHugepagesAllocation** parameter controls whether UPG uses hugepages for heap, stats, and buffers. If set to `true`, UPG uses hugepages instead of standard memory. In this case, **spec.resources.memory** must be at least `1Gi`, and **spec.resources.hugepages-2Mi** must be at least `4Gi`. If set to `false`, hugepages are disabled, and **spec.resources.memory** must be at least `heapsize + 1G`.

6. Set up networking for the UPG instance that includes GRX and Sx interfaces. The `sgi` configuration is omitted because a separate interface is added for each pool. See the configuration example:

   ```yaml
   spec:
     instances:
       inst01:
         networking:
           # Access Network
           grx:
             interface: an1
             networkName: access-net
             ipv4:
               addresses:
                 - 198.18.1.21/24
             routes:
               # UE address range
               - to: 10.0.0.0/10
               # IP of the Access Network gateway
               via: 198.18.1.100
           # Control Network
           sx:
             interface: sx1
             networkName: control-net
             mtu: 8930
             ipv4:
               addresses:
                 -  172.18.1.21/24
   ```

   As a result, the `UPGDeployment` CR creates VPPInterface objects with memif interfaces for GRX (`an1`) and SX (`sx1`).

7. TDF use case commonly uses NAT (Network Address Translation). UPG supports two types of NAT: basic and carrier-grade NAT (CG-NAT). For TDF, `cg-nat` should be used instead of basic NAT because it is integrated with PFCP, ensuring that NAT bindings are correctly managed as part of the subscriber session state:

   ```yaml
   spec:
     nat:
       enabled: true
       type: cgnat
   ```

8. To specify NAT pool capacity, use these parameters in the **spec.instances.{UPG_INSTANCE_NAME}.nat.pools.{NAT_POOL_NAME}** section:

   - **start** - start of the NAT IP pool range, for example: `198.19.8.1`
   - **end** - end of the NAT IP pool range, for example: `198.19.8.254`
   - **blocksize** - range of ports assigned to the NAT binding

   See the example:

   ```yaml
   spec:
     instances:
       {UPG_INSTANCE_NAME}:
       ...
         nat:
           pools:
             {NAT_POOL_NAME}:
               ...
               start: 198.19.8.1
               end: 198.19.8.254
               blocksize: 512
   ```

   When the UPF session is deleted, the NAT binding is released, and the port block becomes available for other sessions. The NAT block is immediately available for allocation after release, depending on pool usage.

   If a UE uses all the ports assigned in the NAT binding, new connections are not NATed, and traffic is blocked until some ports are freed up.

   For more information on how to calculate NAT pool capacity, see the [NAT pool capacity](#nat-pool-capacity) section.

   UPG exposes **vpp_nat_pool_capacity** and **vpp_nat_pool_utilization** metrics for monitoring NAT pool usage. For more information, read the [Metrics](https://docs.cennso.com/components/upg/3.2/observability/metrics.md) document.

9. The per-pool configuration for NAT includes configuration for interface settings (name, type, MTU, IP address, and pool gateway address). This configuration generates VPPInterface objects for NAT pool interfaces. Here’s a configuration example for two NAT pools, **pool-de** and **pool-at**:

   ```yaml
   spec:
     instances:
       inst01:
       (...)
         nat:
           pools:
             pool-de:
               networking:
                 interface: de1
                 networkName: "de-net"
                 mtu: 1500
                 ipv4:
                   gw: 198.19.2.1
                   addresses:
                     - 198.19.2.21/24
               start: 198.19.8.1
               end: 198.19.8.254
               blocksize: 512
             pool-at:
               networking:
                 interface: at1
                 networkName: "at-net"
                 mtu: 1500
                 ipv4:
                   gw: 198.19.3.1
                   addresses:
                     - 198.19.3.21/24
               start: 198.19.16.1
               end: 198.19.16.254
               blocksize: 512
   ```

### NAT pool capacity

The NAT pool's capacity, or the number of User Equipments (UEs) that the UPG can serve, is calculated as follows:

```math
Capacity = External IPs × (MAX\_PORT − MIN\_PORT)
```

By default:

- `MIN_PORT` = 1024
- `MAX_PORT` = 65535

You can configure these values in the **spec.nat** section:

```yaml
spec:
  nat:
    minNatPort: 1024
    maxNatPort: 65535
```

For a NAT pool with two external IPs and the default port range:

```math
2 × (65535 − 1024) = 2 × 64511 = 129022
```

This means the NAT pool can serve up to 129,022 bindings.

The total number of NAT flows is divided into smaller ranges, determined by the **blocksize** parameter. Each port block, combined with an external IP, creates a NAT binding. For example, a NAT binding might look like this:

```bash
  FRAMED: 100.71.3.248
  EXTERNAL: 198.19.8.1
  port start 1024 port end 1536
```

### Additional configuration

This section describes optional parameters that you can use to further customize the UPG deployment.

#### Affinity

UPG requires a node with a running Fast Path Provider (FPP). Configure **spec.affinity** to ensure that UPG runs on the correct node. For example, you can label all nodes with FPP with the label `fpp-manager.cennso.com/vppswitch: ""`. In this case, the configuration looks like this:

```yaml
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: fpp-manager.cennso.com/vppswitch
            operator: Exists
```

From all the nodes with a running FPP, you can choose a specific node to deploy UPG. In this case, use the node name label, for example `worker-node1.test.cluster.net`:

```yaml
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
          - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
                - worker-node1.test.cluster.net
            - key: fpp-manager.cennso.com/vppswitch
              operator: Exists
```

#### IPFIX

IPFIX (IP Flow Information Export) is used to export flow-related information to a collector. You can configure IPFIX exporters and per-pool IPFIX policies.

To set up **ipfixPolicy** for TDF, use the `NatEvent` template. This example shows the IPFIX exporters configuration for **pool-de** and **pool-at**:

```yaml
spec:
  ipfixExporters:
    - exporterID: pool-de
      collectorAddress: 172.18.1.100
      collectorPort: 4739
      pathMTU: 1420
      srcNetwork: sx
      templateInterval: 15
      udpChecksum: false
    - exporterID: pool-at
      collectorAddress: 172.18.1.101
      collectorPort: 4739
      pathMTU: 1420
      srcNetwork: sx
      templateInterval: 15
      udpChecksum: false
```

The per-pool configuration for NAT includes configuration for IPFIX capabilities:

```yaml
spec:
  instances:
    inst01:
    ...
      nat:
        pools:
          pool-de:
            networking:
              ipfixCollector: 172.18.1.100
              ipfixPolicy: NatEvent
              observationDomainID: 21
              observationDomainName: tdf.de-inst01
              observationPointID: 1
              ipfixReportInterval: 10
              ...
```

#### Rate limiter

You can throttle the session creation rate in the TDF use case to avoid overloading the SMF. The rate limiter applies to the PFCP Session Report Requests sent from UPG to SMF. The **spec.rateLimiterPPS** parameter sets the number of allowed messages per second:

```yaml
spec:
  rateLimiterPPS: 150
```

#### PFCP Heartbeats

You can configure PFCP heartbeat parameters:

```yaml
spec:
  pfcpHeartbeatTimeout: "10"
  pfcpHeartbeatRetries: "5"
```

#### NAT timeouts

You can configure the NAT flow idle timeout to release resources. The timeout depends on the protocol and can be set using `UPGDeployment` CR values:

```yaml
spec:
  nat:
    timeouts:
      icmp: 60             # in seconds, by default 1 minute
      tcpEstablished: 1800 # in seconds, by default 30 minutes
      tcpTransitory: 240   # in seconds, by default 4 minutes
      udp: 300             # in seconds, by default 5 minutes
```

## Verification

To check if the UPGDeployment is installed and ready, run:

```bash
kubectl get upgdeployments.fpp.cennso.com -n cennso-infra upg-tdf
```

Sample output:

```bash
NAME                       SYNCED   READY   CONNECTION-SECRET   AGE
upg-tdf                    True     True                        3m
```

You can also check the status of the VPPDeployment resource that the `UPGDeployment` CR created in the Core Cluster:

```bash
kubectl get vppdeployment -n tdf-example
```

Sample output:

```bash
NAME                       READY   IMAGE                                                AGE
upg-tdf-inst01             True    registry.cennso.com/cennso/upg/vpp-operator:v3.0.0   3m
upg-tdf-inst02             True    registry.cennso.com/cennso/upg/vpp-operator:v3.0.0   3m
```

To verify if UPG TDF setup is working, check following places:
- Read [CG-NAT metrics documentation](https://docs.cennso.com/components/upg/3.2/observability/metrics.md#cg-nat-metric-labels)
- Run [VPPCTL for TDF settings](https://docs.cennso.com/components/upg/3.2/troubleshooting/verification.md#check-tdf)
- See [NAT pools allocation](https://docs.cennso.com/components/upg/3.2/troubleshooting/verification.md#check-cg-nat)
