> 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/upf-nat.md).

# UPF NAT

UPG allows the creation of UPF NAT as an additional NAT type `upf`, alongside `basic` and `cgnat`. While UPF NAT and CG-NAT shares the same dataplane, it does not involve the SMC in selecting the NAT Pool directly. Instead, the NAT Pool is selected based on the Forwarding Policy selected by the SMC.

This document details the behavior, configuration, and current limitations of UPF NAT.

## Configuration

To utilize UPF NAT, you must deploy UPG using the `UPGDeployment`. NAT is configured on the Core Cluster using additional Custom Resources: `UPGNatPool` and `UPGForwardingPolicy` - those objects are created based on **spec.instances.{NAME}.nat.pools** and **spec.instances.{NAME}.forwardingPolicies**. Although NAT Pools can be added dynamically, removing them is not currently recommended. Forwarding Policies, however, can be added and removed without impacting the dataplane.

### UPG Configuration

Below is an overview of the UPG configuration using the `UPGDeployment` object:

```yaml
apiVersion: fpp.cennso.com/v1alpha1
kind: UPGDeployment
metadata:
  name: upg-nat
  namespace: cennso-infra
spec:

  # Select UPG Composition version 3.0.6 or later
  compositionRef:
    name: upg-generic
  compositionUpdatePolicy: Automatic
  compositionSelector:
    matchLabels:
      cennso.com/version: 3.0.6

  # Core Cluster connection params
  connectionSecretRef:
    name: core-cluster
    namespace: cennso-infra
    key: kubeconfig

  # Basic UPG configuration
  version: 3.0.6
  targetNamespace: gmcp-pgw
  operatorName: vpp-operator
  imagePullSecrets:
    - name: cennso-pull-secret
  type: pgw
  labels:
    apn: pgw.test.cennso
  logLevel: info

  # resource configuration
  buffersPerNuma: 128000
  defaultDataSize: 3328
  enableHugepagesAllocation: false
  heapsize: 1900M
  resources:
    cpu: "1"
    hugepages-2Mi: 0Gi
    memory: 4Gi

  # General NAT Configuration (static)
  nat:
    enabled: true
    type: upf
    maxNatPort: 65535
    minNatPort: 1024
    timeouts:
      icmp: 60
      tcpEstablished: 1800
      tcpTransitory: 240
      udp: 300


  # UPG instance configuration
  instances:
    prox01:
      networking:
        grx:
          interface: grx
          ipv4:
            addresses:
            - 198.18.0.20/24    # internal, self-assigned IP
          mtu: 1400
          networkName: pgw-grx
        sx:
          interface: sxb
          ipv4:
            addresses:
            - 172.20.0.20/24    # internal, self-assigned IP
          mtu: 1400
          networkName: pgw-sx
      # no `sgi` here — with nat.type:upf the SGi interface is defined by the NAT-pool networking below.

      # Forwarding Policies configuration - editable at runtime
      forwardingPolicies:
      - policy-au:
          natPools:
          - pool-au
      - policy-de:
          natPools:
          - pool-de

      # NAT Pool configuration - you can add new pools at runtime,
      # modification is not allowed, deletion will not drop traffic
      # if interface is present in other NAT Pool. Adding new NAT Pool
      # after deletion with overlapping IP range will drop traffic from
      # existing sessions
      nat:
        pools:
          pool-au:
            blocksize: 64
            start: 10.10.1.29       # NATed IP address, visible in the external Network
            end: 10.10.1.29
            networking:
              interface: sgi
              ipv4:
                addresses:
                - 198.19.0.20/24    # internal, self-assigned IP
                gw: 198.19.0.1      # sgi next-hop = CGW sgi addr
              mtu: 1400
              networkName: pgw-sgi

          pool-de:
            blocksize: 64
            start: 10.10.1.30       # NATed IP address, visible in the external Network
            end: 10.10.1.30
            networking:
              interface: sgi
              ipv4:
                addresses:
                - 198.19.0.20/24    # internal, self-assigned IP
                gw: 198.19.0.1      # sgi next-hop = CGW sgi addr
              mtu: 1400
              networkName: pgw-sgi

```

This example configures standard UPG settings while also incorporating the NAT Pool and Forwarding Policies configurations detailed in the subsequent sections.

### NAT Configuration

The global NAT configuration is defined within the **spec.nat** field of the `UPGDeployment`:

```yaml
spec:
  nat:
    enabled: true
    type: upf
    maxNatPort: 65535
    minNatPort: 1024
    timeouts:
      icmp: 60
      tcpEstablished: 1800
      tcpTransitory: 240
      udp: 300

```

These settings cannot be edited at runtime; any modifications will trigger a restart of the UPG instances.

The **spec.nat.enabled** flag activates NAT, while the **spec.nat.type** field specifies the NAT type (which must be set to `upf`).

The **spec.nat.maxNatPort** and **spec.nat.minNatPort** parameters define the port range (minimum and maximum) used to create a NAT binding, which is a tuple consisting of an IP address and port range for translating User Equipment traffic.

Per-protocol session idle timeouts, measured in seconds, are configured under the **spec.nat.timeouts** section:

* **icmp**: The ICMP idle timeout (default is `60`).
* **tcpEstablished**: The TCP established idle timeout (default is `1800`).
* **tcpTransitory**: The TCP transitory idle timeout (default is `240`).
* **udp**: The UDP idle timeout (default is `300`).

### NAT Pool Configuration

The **spec.instances.{NAME}.nat.pools** section in the **UPGDeployment** configures the NAT Pools utilized by connected User Equipment. Each NAT Pool is associated with an NWI via the interface name.

Multiple NAT Pools may share a single interface on the SGi side. Every NAT Pool must define a complete networking section, encompassing IP addresses, MTU, and FPPNetwork configurations. These settings must be identical across all NAT Pools sharing the interface. This requirement ensures that if one NAT Pool is deleted, the remaining pools retain their interface configuration without disruption.

`mssClamping` and `ueFlowsLimit` are mutable on any pool, and `shared` pools also allow the address range (`start` and `end`) to change. These updates apply at runtime without recreating the pool. Changing `mssClamping` or `ueFlowsLimit` keeps sessions intact, while removing addresses from a `shared` pool's range terminates the sessions that use them. Any other change requires deleting the pool and deploying a replacement with the updated parameters. Deleting a NAT Pool releases every UE session bound to it. The UPF terminates each affected session and reports it to the Control Plane. Sessions bound to other NAT Pools are not affected.

Each NAT Pool has an optional `mode` that controls how external ports are allocated (the default is `cgnat`):

* `cgnat`: reserves a port block of `blocksize` ports per UE. All flows from a UE keep the same external address and port block.
* `shared`: dynamically allocates ports per flow from the pool's whole port range (`minNatPort` to `maxNatPort`). All flows from a UE keep the same external address, but the external port can differ between flows.

The `mode` is fixed once the NAT Pool is created.

`shared` pools also support `ueFlowsLimit`, which caps the number of concurrent TCP/UDP NAT flows per UE. The default is `64`, and `0` removes the limit. It has no effect on `cgnat` pools.

Set `mssClamping` on a pool to clamp the TCP MSS of its NAT traffic to the given value. `0` disables clamping. It applies to both `cgnat` and `shared` pools.

Below is a sample NAT Pool configuration:

```yaml
spec:
  # UPG instance configuration
  instances:
    prox01:
      nat:
        pools:
          pool-au:
            mode: shared            # cgnat (default) or shared
            ueFlowsLimit: 64        # shared pools only, 0 = unlimited
            mssClamping: 1360       # clamp TCP MSS, 0 = disabled
            blocksize: 64
            start: 10.10.1.29       # NATed IP address, visible in the external Network
            end: 10.10.1.29
            # Each pool needs to provide interface details
            networking:
              interface: sgi
              ipv4:
                addresses:
                - 198.19.0.20/24    # internal, self-assigned IP
                gw: 198.19.0.1      # sgi next-hop = CGW-SGI address
              mtu: 1400
              networkName: pgw-sgi

          pool-de:
            blocksize: 64
            start: 10.10.1.30       # NATed IP address, visible in the external Network
            end: 10.10.1.30
            # Each pool needs to provide interface details
            networking:
              interface: sgi
              ipv4:
                addresses:
                - 198.19.0.20/24    # internal, self-assigned IP
                gw: 198.19.0.1      # sgi next-hop = CGW-SGI address
              mtu: 1400
              networkName: pgw-sgi

```

### Forwarding Policies Configuration

Forwarding Policies are defined within the **spec.instances.{NAME}.forwardingPolicies** section of the `UPGDeployment`. These policies can be dynamically added, removed, or modified at runtime. The configuration is applied to the UPG immediately without requiring a restart, taking effect on live traffic.

Each Forwarding Policy name must be unique within a single `UPGDeployment` instance (e.g., `policy-au` and `policy-de` in the example below). Additionally, all NAT Pools referenced under **spec.instances.{NAME}.forwardingPolicies.{POLICY-NAME}.natPools** must be explicitly defined in the **spec.instances.{NAME}.nat.pools** section.

```yaml
spec:
  # UPG instance configuration
  instances:
    prox01:
      forwardingPolicies:
      - policy-au:
          natPools:
          - pool-au
          - pool-au-2
      - policy-de:
          natPools:
          - pool-de

```

UPG distributes new sessions across the policy's NAT Pools in proportion to each pool's capacity (the number of external addresses in its range). In `cgnat` mode, UPG skips a pool once its port blocks are exhausted. A `shared`-mode pool draws ports per flow, so UPG never considers it exhausted.

## Verification

To verify that the `UPGDeployment` is successfully installed and ready, execute the following command on the Management Cluster:

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

Sample output:

```bash
NAME            SYNCED   READY   CONNECTION-SECRET   AGE
upg-nat         True     True                        2h
```

You can also verify the status of the `VPPDeployment` resource, which is generated by the `UPGDeployment` Custom Resource in the Core Cluster (using the namespace defined in **spec.targetNamespace**, such as `gmcp-pgw`):

```bash
kubectl get vppdeployment -n gmcp-pgw
```

Output:

```
NAME                   READY   IMAGE                                                   AGE
upg-nat-prox01         True    registry.cennso.com/cennso/vpp-worker-upg:v3.0.6        2h
```

### NAT Pool and Forwarding Policy configuration

On the Core Cluster, you can access the UPG pod via `exec` to run the following diagnostic commands.

#### NAT Pools

To inspect the NAT Pool configuration:

```bash
vppctl show upf nat pool
```

Sample output:

```
id 0 name 'pool-au' addr 10.10.1.29-10.10.1.29 ports {size 64 in 1024-65535} blocks usage 0/1008 (timeout 0) intf default nwi 'sgi' refs 1
id 1 name 'pool-de' addr 10.10.1.30-10.10.1.30 ports {size 64 in 1024-65535} blocks usage 0/1008 (timeout 0) intf default nwi 'sgi' refs 1
```

NAT Pool statistics are exposed through the following UPG metrics:

* `vpp_nat_pool_info{name="pool-au",network_instance="sgi"} 1`
* `vpp_nat_pool_utilization{name="pool-au",network_instance="sgi"} 0`
* `vpp_nat_pool_block_size{name="pool-au",network_instance="sgi"} 64`
* `vpp_nat_pool_capacity{name="pool-au",network_instance="sgi"} 1008`
* `vpp_nat_pool_bytes_total{direction="i2o",name="pool-au",thread="0"} 30660`
* `vpp_nat_pool_bytes_total{direction="o2i",name="pool-au",thread="0"} 30576`
* `vpp_nat_pool_flow_count{name="pool-au",thread="0",type="icmp"} 0`
* `vpp_nat_pool_flow_count{name="pool-au",thread="0",type="tcpudp"} 0`

#### Forwarding Policies

To view the Forwarding Policies, run the following command within the UPG pod:

```bash
vppctl show upf policy
```

Sample output:

```
id:0 policy:policy-au ip4_fib:-1 ip6_fib:-1 refs:0 nat-pools:pool-au
id:1 policy:policy-us ip4_fib:-1 ip6_fib:-1 refs:0 nat-pools:pool-us
```

#### Core Cluster configuration objects

Additionally, you can monitor the status of Kubernetes API objects directly on the Core Cluster. Events related to the creation or deletion of the `UPGNatPool` and `UPGForwardingPolicy` Custom Resources are published there. Examples include:

* **Successful NAT Pool Creation**:

  ```
  Events:
    Type    Reason           Age   From          Message
    ----    ------           ----  ----          -------
    Normal  UPFNatPoolApply  106s  VPPInterface  NAT Pool "pool-au" creation successful
  ```

* **Successful Forwarding Policy Creation**:

  ```
  Events:
    Type    Reason          Age    From          Message
    ----    ------          ----   ----          -------
    Normal  UPFPolicyApply  77s    VPPInterface  Forwarding Policy "policy-au" creation successful
  ```

* **Failed Forwarding Policy Modification (Missing NAT Pool)**:

  ```
  Events:
    Type     Reason             Age                     From          Message
    ----     ------             ----                    ----          -------
    Normal   UPFPolicyDelete    4m59s (x15 over 6m16s)  VPPInterface  Forwarding Policy "policy-au" deletion successful
    Warning  ErrUPFPolicyApply  4m59s (x15 over 6m16s)  VPPInterface  Forwarding Policy "policy-au" creation err: "attaching NAT pool \"pool-asd\" (index 1) to policy \"policy-au\": UpfPolicyNatPoolAddDel failed: req &{IsAdd:true Index:1
  Name:pool-asd Policy:policy-au} reply {Retval:0}: RPC call failed: VPPApiError: No such entry (-6)"
  ```

In case of an error, events will be published at regular intervals. If no events are present for a specific object, it indicates that the configuration was either applied successfully and the event expired or that it was never applied.

### UPG NAT traffic

To verify that traffic is passing through the NAT, you can list the active NAT flows. Execute the following commands on the UPG pod:

* ICMP flows
  ```bash
  vppctl show upf nat icmp flows
  ```

  Sample output:

  ```
  [w 0] id 0 {in 10.16.101.19 out 198.19.0.100 nat 10.100.4.23} icmp_id {og 116 nat 1076} bind {id 62 sid 1} last time 1020
  [w 0] id 1 {in 10.13.221.38 out 198.19.0.100 nat 10.100.4.28} icmp_id {og 116 nat 1076} bind {id 57 sid 2} last time 1017
  [w 0] id 2 {in 10.3.247.37 out 198.19.0.100 nat 10.100.4.22} icmp_id {og 116 nat 1076} bind {id 63 sid 3} last time 1019
  [w 0] id 5 {in 10.16.87.231 out 198.19.0.100 nat 10.100.4.30} icmp_id {og 116 nat 1076} bind {id 55 sid 6} last time 1019
  ```

* TCP/UDP flows:

  ```bash
  vppctl show upf nat tcpudp flows
  ```

  Sample output:

  ```
  [w 0][id 0] o2i {in 10.100.4.43:1031 out 198.19.0.100:80} proto 6 bind {id 62 sid 12} upf_flow 19
  [w 0][id 1] o2i {in 10.100.4.42:1071 out 198.19.0.100:80} proto 6 bind {id 63 sid 20} upf_flow 11
  [w 0][id 2] o2i {in 10.100.4.45:1051 out 198.19.0.100:80} proto 6 bind {id 60 sid 6} upf_flow 14
  ```
