> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/fpp/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/fpp/3.2/deployment-guides/upflb.md).

# Set up UPF load balancing

> [!IMPORTANT]
> User Plane Function (UPF) load balancing is an alpha feature and is not suitable for production deployments.

Fast Path Provider (FPP) 3.2.0 can run a Connectivity Gateway (CGW) as a UPF load balancer (UPF-LB) for a pool of UPF instances.
UPF-LB sends the traffic of each session to the UPF that serves it.
An external controller places each session on a UPF and publishes the placement in a NATS JetStream key-value (KV) bucket.
An example of such a controller is the [UPF Session Controller (UPF-SC)](https://docs.cennso.com/components/upg/3.2/deployment-guides/upf-high-availability.md).
UPF-LB watches the bucket and applies each change to the Vector Packet Processing (VPP) forwarding tables.

UPF-LB supports IPv4 only.

## Prerequisites

Before you set up UPF load balancing, make sure that you have:

- VPP Operator 3.2.0, upgraded as described in [3.1.0 -> 3.2.0](https://docs.cennso.com/components/fpp/3.2/releases/3.2.0.md#310---320)
- A `CGWDeployment` on the FPP 3.2.0 revision of the `cgw-generic` Composition
- NATS with JetStream enabled
- A KV bucket that a controller writes to

## How it works

UPF-LB handles traffic per Network Instance (NWI).
Each NWI has an external side, facing the peer network, and an internal side, facing the UPF pool.
Each side has its own VPP table.
The following table describes the two NWI types:

| NWI type | External side | Uplink | Downlink |
|---|---|---|---|
| `access` | GPRS Tunneling Protocol User Plane (GTP-U) peers, for example the gNodeB (gNB) | Selects the UPF by the GTP-U Tunnel Endpoint Identifier (TEID) and rewrites the outer destination address to that UPF | Rewrites the outer source address to the NWI's **ownIP** |
| `sgi` | Data network | Forwards to the data network without changes | Routes each User Equipment (UE) address to its UPF through a `/32` route |

UPF-LB answers GTP-U echo requests to the **ownIP** of an `access` NWI itself.
Traffic that is not GTP-U or not for a UE pool follows the normal routes of the table.

### KV bucket key format

The controller writes one key for each TEID or UE address:

```text
<gtpu|ip4d>.<nwi>.<access|sgi>.<selector>  ->  {"next":"<UPF IPv4 address>"}
```

The key kinds and selectors work as follows:

- `gtpu` keys use the `access` side and a TEID as the selector.
- `ip4d` keys use the `sgi` side and a UE IPv4 address as the selector, with underscores in place of the dots, for example `203_0_113_10`.
- Selector `0` is the catch-all of the NWI for traffic that matches no other key.

Without a catch-all, UPF-LB drops the traffic that matches no key.

UPF-LB watches only `gtpu.>` and `ip4d.>`, so other applications can share the bucket.
After a reconnect, UPF-LB replays the bucket and removes the entries that the controller deleted while UPF-LB was disconnected.

## Configure the CGWDeployment

Add the UPF-LB configuration to a `CGWDeployment`. Give the CGW access to NATS.

1. If NATS requires authentication, create a Secret with the credentials file in the CGW namespace on the Core Cluster.
   Store the file under the `user.creds` key:

   ```bash
   kubectl create secret generic upflb-nats-creds -n {CGW_NAMESPACE} --from-file=user.creds={NATS_CREDS_FILE}
   ```

2. Configure UPF-LB with **spec.upflbConfig.spec** of the `CGWDeployment`.
   A non-empty spec enables UPF-LB in the CGW and creates a `UPFLBConfig` resource in the Core Cluster:

   ```yaml
   apiVersion: fpp.cennso.com/v1alpha1
   kind: CGWDeployment
   metadata:
     name: cgw-lb
   spec:
     version: 3.2.0
     connectionSecretRef:
       name: test-core-cluster
       namespace: cennso-infra
     ...
     upflbConfig:
       spec:
         nats:
           url: nats://nats.nats-system:4222
           bucket: upflb
           creds: /etc/upflb/nats
         nwis:
         - name: epc
           interface: access
           externalTableID: 10
           internalTableID: 20
           ownIP: 198.51.100.20
         - name: sgi
           interface: sgi
           externalTableID: 30
           internalTableID: 40
         uePools:
         - nwiName: sgi
           cidr: 203.0.113.0/25
         interfaces:
         - access
         - access-internal
         - sgi
         - sgi-internal

     additionalVolumes:
     - name: upflb-nats
       secret:
         secretName: upflb-nats-creds
     additionalVolumeMounts:
     - name: upflb-nats
       mountPath: /etc/upflb/nats
       readOnly: true
   ```

   If the NATS server does not require authentication, omit the following fields:

   - **nats.creds**
   - **additionalVolumes**
   - **additionalVolumeMounts**

   The manifest also needs the `RoutingTable` and `VPPInterface` resources of both sides in **spec.routingTables** and **spec.vppInterfaces**, as for any other CGW.

3. Apply the `CGWDeployment` on the Management Cluster:

   > [!CAUTION]
   > If you apply this configuration to an existing CGW, the CGW pods restart, and traffic through the CGW stops until the pods are ready.

   ```bash
   kubectl apply -f {CGW_DEPLOYMENT_FILE}
   ```

The CGW pod starts and becomes ready after UPF-LB receives a full copy of the bucket.

The following table describes the **spec.upflbConfig.spec** parameters:

| Parameter | Description |
|---|---|
| **nats.url** | URL of the NATS server. Required. |
| **nats.bucket** | Name of the JetStream KV bucket that the controller writes to. UPF-SC writes `UPFLB` by default. Required. |
| **nats.domain** | JetStream domain. Optional. |
| **nats.creds** | Path in the `fpp` container to the NATS credentials file, or to a directory that contains it as `user.creds`. Leave empty to connect without authentication. |
| **nwis[].name**, **nwis[].interface** | Name and type (`access` or `sgi`) of the NWI. One NWI name can have one entry of each type. |
| **nwis[].externalTableID**, **nwis[].internalTableID** | VPP table IDs of the two sides. Both tables must exist and differ, and each table can belong to one NWI only. |
| **nwis[].externalTableName**, **nwis[].internalTableName** | Alternative to the table IDs: the full name of a `RoutingTable` resource in the CGW namespace. |
| **nwis[].ownIP** | IPv4 address of the NWI toward the GTP-U peers. Required for `access`, not allowed for `sgi`. |
| **uePools[]** | UE address pools served by the `sgi` NWI. For each pool, UPF-LB installs a covering route to the `ip4d` catch-all. If the NWI has no catch-all, UPF-LB drops the traffic. |
| **interfaces** | `VPPInterface` **ifName** values on which UPF-LB processes traffic. List the interfaces of both the external and the internal sides. |

## Readiness and metrics

A new CGW pod stays not ready until UPF-LB receives a full copy of the bucket.
This way, the pod never takes traffic without a forwarding table.
After that, the pod stays ready even if NATS becomes unavailable, and keeps forwarding with the entries it holds.

The CGW exports UPF-LB metrics with the `vpp_upflb_` prefix.
The most important ones are:

| Metric | Description |
|---|---|
| **vpp_upflb_nats_ready** | `1` while UPF-LB is connected to NATS. Alert on `0`. |
| **vpp_upflb_kv_snapshot_complete** | `1` once UPF-LB replays the whole bucket. |
| **vpp_upflb_kv_entries**, **vpp_upflb_kv_entries_installed** | Entries received from the bucket, and entries installed in the forwarding tables. A gap means that some entries refer to an NWI that does not exist yet. |
| **vpp_upflb_kv_updates_total** | KV updates by `result`: `applied`, `rejected`, or `stale`. |
| **vpp_upflb_rewrite_packets_total** | Packets handled by UPF-LB, by `result`, for example `rewritten`, `switched`, or `no_entry`. |

For the full list of UPF-LB metrics, see [UPF-LB metrics](https://docs.cennso.com/components/fpp/3.2/observability/metrics.md#upf-lb-metrics).

## Verification

Run the following commands to check the UPF-LB state in the `fpp` container of the CGW pod on the Core Cluster:

1. Check that UPF-LB is connected to NATS:

   ```bash
   kubectl -n {CGW_NAMESPACE} exec {CGW_POD} -c fpp -- vppctl show upflb nats
   ```

   Sample output:

   ```text
   url:       nats://nats.nats-system:4222
   bucket:    UPFLB
   domain:    <local>
   creds:     /etc/upflb/nats
   ready:     1
   status:    0 (OK)
   snapshot:  1
   ...
   ```

   The `ready` line shows `1` while UPF-LB is connected to NATS.
   The `snapshot` line shows `1` after UPF-LB replays the whole bucket.
   If the `ready` line shows `0`, the `status` line shows the NATS error.

2. List the NWIs:

   ```bash
   kubectl -n {CGW_NAMESPACE} exec {CGW_POD} -c fpp -- vppctl show upflb nwi
   ```

   Sample output:

   ```text
   nwi                              intf      ext-table  int-table own-ip
   epc                              access           10         20 198.51.100.20
   sgi                              sgi              30         40 0.0.0.0

   enabled interfaces:
   ...
   ```

   The output lists each NWI of **spec.upflbConfig.spec.nwis** with its table IDs.
   The `enabled interfaces` list shows the interfaces of **spec.upflbConfig.spec.interfaces**.

3. Check the UE pools:

   ```bash
   kubectl -n {CGW_NAMESPACE} exec {CGW_POD} -c fpp -- vppctl show upflb ue-pool
   ```

   Sample output:

   ```text
   nwi                              prefix               next              inst
   sgi                              203.0.113.0/25       drop               yes
   ```

   The `next` column shows `drop` while the NWI has no `ip4d` catch-all entry.

4. After the controller publishes sessions, list the entries that UPF-LB received from the bucket:

   ```bash
   kubectl -n {CGW_NAMESPACE} exec {CGW_POD} -c fpp -- vppctl show upflb kv
   ```

   Sample output:

   ```text
   key                                          next             intf     selector          inst        rev   gen
   gtpu.epc.access.16777217                     198.51.100.101   access   16777217           yes         12     1
   gtpu.epc.access.16777218                     198.51.100.102   access   16777218           yes         14     1
   ip4d.sgi.sgi.203_0_113_10                    203.0.113.201    sgi      203.0.113.10       yes         13     1
   ip4d.sgi.sgi.203_0_113_11                    203.0.113.202    sgi      203.0.113.11       yes         15     1

   4 entries, snapshot complete
   ```

   UPF-LB holds a forwarding entry for each TEID and each UE address.
   `yes` in the `inst` column means that UPF-LB installed the entry in the forwarding tables.
   The `next` column shows the address of the UPF that serves the entry, on the NWI of the entry.
   The command lists up to 100 entries.
   To list up to `10000` entries, add `limit {N}` to the command.

5. (Optional) List the entries that the Data Plane uses:

   ```bash
   kubectl -n {CGW_NAMESPACE} exec {CGW_POD} -c fpp -- vppctl show upflb map
   ```

When all checks pass, UPF-LB sends the traffic that matches an installed entry to the UPF in the `next` column of that entry.
