> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/upg/2.17/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/2.17/use-cases/tdf.md).

# Traffic Detection Function

Traffic Detection Function (TDF) identifies and categorises traffic flows to enable features, such as application-specific charging, QoS enforcement, and content filtering.

The User Plane Gateway (UPG) acts as the TDF-U (User Plane). It checks traffic flows in real time and enforces policies directly on the data path. The Control Plane functionality (TDF-C) is handled by the Session Management Controller (SMC) component, which manages session configuration, signaling, and policy control to control the User Plane's operations.

![TDF use case](https://docs.cennso.com/api/assets/components/upg/2.17/assets/upg-tdf.svg)

## Configuration

Follow these steps to configure the UPG chart for the TDF use case:

1. Create FPPNetworks for the Access (`access-net`) and Control (`control-net`) network interfaces outside the UPG chart. Also networks per NAT pool (`at-net` and `de-net`) needs to be created to separate the NATed traffic. This enables cross-node connectivity from access net CGW and CGWs on data network side. See the 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
   ```

1. Set the **type** field to `tdf` to specify the use case in which UPG should run.

1. UPG requires a node with running Fast Path Provider (FPP). Configure **nodeAffinity** 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 chart configuration looks like this:

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

   From all the nodes with 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
   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
   ```

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

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

1. If using a private registry, add **imagePullSecrets**.

   ```yaml
   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.

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

   ```yaml
   resources:
     cpu: 2
     memory: 8Gi
   heapsize: "7G"
   enableHugepagesAllocation: false
   ```

   The **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, **resources.memory** must be at least `1Gi`, and **resources.hugepages-2Mi** must be at least `4Gi`. If set to `false`, hugepages are disabled, and **resources.memory** must be at least `heapsize + 1G`. Turning hugepages on or off does not affect UPG's performance or stability.

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

   ```yaml
   instances:
     inst01:
       networking:
         # Access Network
         grx:
           interface: an1
           type: memif
           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
           type: memif
           networkName: control-net
           mtu: 8930
           ipv4:
             addresses:
               -  172.18.1.21/24
   ```

   As a results, the UPG chart creates NADs with memif interfaces for GRX (`an1`) and SX (`sx1`).

1. TDF use case requires NAT (Network Address Translation). UPG supports two types of NAT: basic and carrier-grade NAT (CG-NAT). For TDF, use `cg-nat`:

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

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

   ```yaml
   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
   ```

1. To specify NAT pool capacity, use these parameters in the **instances.{UPG_INSTANCE_NAME}.nat** 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
      instances:
        {UPG_INSTANCE_NAME}:
        ...
          nat:
            pools:
              {NAT_POOL_NAME}:
                ...
                start: 198.19.8.1
                end: 198.19.8.2
                blocksize: 512
      ```

   >**NOTE:** For more information on how to calculate NAT pool capacity, read the [NAT pool capacity](#nat-pool-capacity) section.

   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.

   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/2.17/observability/metrics.md) document.

1. The per-pool configuration for NAT includes configuration for IPFIX capabilities and interface settings (name, type, MTU, IP address, and pool gateway address). This configuration generates Network Attachment Definitions (NADs) for NAT pool interfaces, using the fpp-cni and memif interface types. Here’s a configuration example for two NAT pools, **pool-de** and **pool-at**:

   ```yaml
   instances:
     inst01:
     (...)
       nat:
         pools:
           pool-de:
             networking:
               ipfixCollector: 172.18.1.100
               ipfixPolicy: NatEvent
               observationDomainID: 21
               observationDomainName: tdf.de-inst01
               observationPointID: 1
               ipfixReportInterval: 10
               interface: de1
               type: memif
               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:
               ipfixCollector: 172.18.1.101
               ipfixPolicy: NatEvent
               observationDomainID: 21
               observationDomainName: tdf.at-inst01
               observationPointID: 1
               ipfixReportInterval: 10
               interface: at1
               type: memif
               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
   ```

   The resulting NADs look as follows:

   ```yaml
   apiVersion: k8s.cni.cncf.io/v1
   kind: NetworkAttachmentDefinition
   metadata:
     name: upg-01-tdf-example-inst01-pool-at-at1
     namespace: tdf-example
   spec:
     config: |-
       {
         "cniVersion": "0.3.0",
         "ipam": {
           "addresses":
           [
             {
               "address": "198.19.3.21/24"
             }
           ],
           "type": "static"
         },
         "mtu": 1500,
         "name": "at1",
         "table": 200,
         "type": "fpp-cni",
         "networkName": "at-net",
         "vppIfaceType": "memif"
       }
   ---
   apiVersion: k8s.cni.cncf.io/v1
   kind: NetworkAttachmentDefinition
   metadata:
     name: upg-01-tdf-example-inst01-pool-de-de1
     namespace: tdf-example
   spec:
     config: |-
       {
         "cniVersion": "0.3.0",
         "ipam": {
           "addresses":
           [
             {
               "address": "198.19.2.21/24"
             }
           ],
           "type": "static"
         },
         "mtu": 1500,
         "name": "de1",
         "table": 300,
         "type": "fpp-cni",
         "networkName": "de-net",
         "vppIfaceType": "memif"
       }
   ```

1. The resulting UPGConfig CR is created by the UPG chart. The NWIs per NAT pool are defined and it includes the IPFIX configuration for each NWI:

   ```yaml
   apiVersion: upg-manager.cennso.com/v1alpha1
   kind: UPGConfig
   metadata:
     name: upg-01-tdf-example-inst01
     namespace: tdf-example
   spec:
     nwis:
     - ipv4TableID: 0
       ipv6TableID: 0
       name: cp
     - ipv4TableID: 100
       ipv6TableID: 100
       name: access
     - ipfixCollectorAddress: 172.18.1.101
       ipfixPolicy: NatEvent
       observationDomainID: 21
       observationDomainName: tdf.at-inst01
       observationPointID: 1
       ipfixReportInterval: 10
       ipv4TableID: 200
       ipv6TableID: 200
       name: at1
     - ipfixCollectorAddress: 172.18.1.100
       ipfixPolicy: NatEvent
       observationDomainID: 21
       observationDomainName: tdf.de-inst01
       observationPointID: 1
       ipfixReportInterval: 10
       ipv4TableID: 300
       ipv6TableID: 300
       name: de1
   ```

   NAT pools configuration is included in VPPConfig with per UPG target:

   ```yaml
   ---
   apiVersion: fpp-manager.cennso.com/v1alpha1
   kind: VPPConfig
   metadata:
     labels:
       fpp-manager.cennso.com/plumbing-instance: upg-01-tdf-example-inst01
     name: upg-01-tdf-example-inst01
     namespace: tdf-example
   spec:
     afterCommands:
     - set ip neighbor-config ip4 age 10
     - set ip neighbor-config ip6 age 10
     - upf gtpu endpoint ip 172.18.1.21 nwi cp teid 0x80000000/2
     - upf tdf ul table vrf {{ (index .Ifs "an1").Table }} ip4 table-id 1001
     - upf tdf ul enable ip4 {{ (index .Ifs "an1").Name }}
     - upf nat pool 198.19.16.1 - 198.19.16.254 block_size 512 nwi at1 name pool-at
     - upf ueip pool nwi at1 id pool-at
     - upf nat pool 198.19.8.1 - 198.19.8.254 block_size 512 nwi de1 name pool-de
     - upf ueip pool nwi de1 id pool-de
     - upf specification release 16
     - upf node-id fqdn upg-01-tisp-ref-inst01
     - upf pfcp server set fifo-size 512
     - upf pfcp endpoint ip 172.18.1.21 vrf 0
     - upf pfcp policer set cir-pps 150 cb-ms 1000
     interfaces:
     - an1
     - sx1
     - at1
     - de1
   ```

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

   ```yaml
   ipfixExporters:
     - exporterID: pool-de
       collectorAddress: 172.18.1.100
       collectorPort: 4739
       pathMTU: 1420
       srcNetwork: sx
       templateInterval: 5
       udpChecksum: false
     - exporterID: pool-at
       collectorAddress: 172.18.1.101
       collectorPort: 9374
       pathMTU: 9000
       srcNetwork: sx
       templateInterval: 10
       udpChecksum: true
   ```

For complete configuration details, see `values.yaml` files for these configuration examples:

- [TDF UPG config](https://docs.cennso.com/api/assets/components/upg/2.17/assets/example-config/tdf-values.yaml)

### 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 **instances.{UPG_INSTANCE_NAME}.nat** section:

```yaml
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
```

### 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 **rateLimiterPPS** parameter sets the number of allowed messages per second:

```yaml
rateLimiterPPS: 150
```
