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

# UPG deployment guide

## UPG Deployment

`upg-manager` image uses `fpp-manager` as base image. This includes Golang
based controller for `VPPInterface`, `VPPConfig`, `IPFIXExporter` and
`RoutingTable` Custom Resources (CRs). UPG registers to watch for this resources
on Kubernetes API. Besides that, UPG does reconciliation for `UPGConfig` CR.

This mechanism uses Kubernetes Custom Resources to configure interfaces, routing
tables, PFCP Server parameters and NWIs (network instances) in VPP instance.
Any CLI commands referring to this resources will be revoked and align with k8s
API state.

General supported deployment model is using [helm chart](../helm/README.md)
included in this repo.

Steps to deploy UPG:

1. Deploy `fpp-crd` chart

1. Deploy `upg-crd` chart

1. Deploy `upg-support` chart

1. Deploy `upg` chart

### Deploy `fpp-crd`
`fpp-crd` chart deployment is needed because UPG uses `VPPInterface` and others
FPP Custom Resources for configuration. This chart does not require any
additional configuration and should be deployed once per cluster.

### Deploy `upg-crd`
`upg-crd` helm chart provides UPG specific Custom Resources, which needs to be
installed for proper `upg-manager` functioning. This chart does not require
any additional configuration and should be deployed once per cluster.

This mean that UPG CRDs are shared between all UPG deployments in the cluster.

### Deploy `upg-support`
`upg-support` chart provides Prometheus `ServiceMonitor` and `UPG Overview`
Grafana dashboard.

Current limitation is that `UPG Overview` dashboard should be installed once
in the cluster - it gathers metrics from multiple `upg` instances. Installing
it twice may influence the Grafana Operator to flip the ownership and location
of the dashboard.

Example of chart configuration:

```
dashboard:
  enabled: true
  prometheusDatasourceName: "Prometheus"

prometheus:
  servicemonitor:
    enabled: true
    namespaceSelector:
      any: false
```

To deploy `upg-supprt` without `UPG Overview` dashboard, simply does include
`dashboard.enabled: false` in chart's `values.yaml`.

### Deploy `upg`
`upg` chart deploys `upg-manager` with proper configuration. This chart can be
installed multiple times in the same cluster.

Example configuration values:

- [PGW example values.yaml](https://docs.cennso.com/api/assets/components/upg/2.2/example-config/pb-routing-values.yaml)
- [TDF example values.yaml](https://docs.cennso.com/api/assets/components/upg/2.2/example-config/tdf-values.yaml)

## General UPG configuration

Based on chart configuration, below settings are included in UPG deployment.

UPG manager acts like VPP manager - generates `startup.conf` and starts vpp as
a child process. To start `upg-manager`, following environment variable should
be defined:

- `FPP_DISABLE_UPLINK` - set to any value will instruct manager that it should
not use DPDK based physical interfaces, for upg `memifs` can be used

- `FPP_INSTANCE_NAME` - set UPG name, used for watching resources labeled with
`fpp-manager.cennso.com/plumbing-instance`, both values needs to match

- `FPP_VPP_RUN_PATH` - define vpp runtime directory, by default set to
`/run/vppswitch`, which is used in `fpp-manager`. Needs to be set not to use
the same directory that `fpp-manager`

- `FPP_INSTANCE_NAMESPACE` - set namespace where `upg-manager` watches for CRs

- `FPP_INSTANCE_UID` - contains pod UID value, distinguish between pod
generations and used in label `fpp-manager.cennso.com/plumbing-instance-uid`

- `FPP_HEAP_SIZE` - declares heap size for UPG instance, by default set to 6G
by manager

- `FPP_STATS_SIZE` - declares stats segment size for metrics, default 512M

UPG handles configuration based on VPP CLI commands with
upf plugin extension. UPF plugin consists of following:

- definition of 3GPP TS 29.244 standard implementation
(CLI: `upf specification release 16`)

- Node name, used in control plane communication:
(CLI: `upf node-id fqdn <node-name>`)

- PFCP server configuration:
  - CLI: `upf pfcp endpoint ip <ip-sx-interface> vrf <control-plane-table-id>`
  - CLI: `upf pfcp server set fifo-size 512`

- Define tunnel endpoint id:
  - CLI: `upf gtpu endpoint ip <ip-sx-interface> nwi <cp-nwi-name> teid 0x80000000/2`
  - if running in PGW mode, CLI: `upf gtpu endpoint ip <ip-grx-interface> nwi epc teid 0x80000000/2`

There are also general VPP commands used to configure UPG:

- Setting up expiration time for ip neighbors per address family:
  - CLI: `set ip neighbor-config ip4 age 10`
  - CLI: `set ip neighbor-config ip6 age 10`

CLI commands should be executed using `afterCommands` field in `VPPConfig.spec` CR:

```yaml
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: VPPConfig
(...)
spec:
  afterCommands:
  - set ip neighbor-config ip4 age 10
  - set ip neighbor-config ip6 age 10
  - upf specification release 16
  - upf node-id fqdn upg-fpp-inst01
  - upf pfcp server set fifo-size 512
  - upf pfcp endpoint ip 172.20.0.20 vrf 0
  - upf gtpu endpoint ip 172.20.0.20 nwi cp teid 0x80000000/2
  - upf gtpu endpoint ip 198.18.0.20 nwi epc teid 0x80000000/2
```

### Additional config

If specific, non-standard CLI commands needs to be executed per UPG instance,
it can be defined in `additionalConfig` field in `values.yaml`:

```yaml
instances:
  inst01:
  (...)
    additionalConfig:
      - ip table add 201
      - ip table add 202
      - upf policy add id defaultInet via ip4-lookup-in-table 201
      - upf policy add id defaultVpn via ip4-lookup-in-table 202
```

## Interface creation

UPG uses `VPPInterface` CR to handle interface creation. For VPP-based
workloads, most suitable interface type is `memif` - in memory interface,
that connects underlying VPP switch with UPG.

`VPPInterface` can be created manually, by higher level UPG operator or
by NetworkAttachmentDefinition (NAD) with `fpp-cni` as an interface type.
NADs are created by helm chart provided with UPG Manager for interfaces:

- `grx` - for access network interface
- `sx` - for control plane communication
- `sgi` - for data network connectivity

As an example of `grx` interface, helm chart configuration:

```yaml
instances:
  inst01:
    networking:
      grx:
        type: memif
        vni: 90
        mtu: 9000
        interface: grx
        ipv4:
          addresses:
           - 198.18.0.20/24
          routes:
            - to: 0.0.0.0/0
              via: 198.18.0.1
```

Resulting NAD:

```yaml
apiVersion: k8s.cni.cncf.io/v1
kind: NetworkAttachmentDefinition
metadata:
  name: upg-inst01-grx
  namespace: upg-fpp
spec:
  config: |-
    {
      "cniVersion": "0.3.0",
      "ipam": {
        "addresses":
        [
          {
            "address": "198.18.0.20/24"
          }
        ],
        "type": "static"
      },
      "mtu": 9000,
      "name": "grx",
      "table": 100,
      "type": "fpp-cni",
      "vni": 90,
      "vppIfaceType": "memif"
    }
```

Static IP address assignment is used here, and resulting `VPPInterface`:

```yaml
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: VPPInterface
metadata:
  finalizers:
  - fpp-manager.cennso.com/vpp-interface-finalizer
  labels:
    fpp-manager.cennso.com/plumbing-container-id: a65e91ee578185d4e822fcb8be3c8ccc31b775b5
    fpp-manager.cennso.com/plumbing-ifname: grx
    fpp-manager.cennso.com/plumbing-instance-uid: 64f8b1a1-2373-4dcd-8801-539cdec505d2
    fpp-manager.cennso.com/plumbing-memif-role: slave
    fpp-manager.cennso.com/plumbing-pod-name: upg-inst01-7fc97c76bb-s5vq5
    fpp-manager.cennso.com/plumbing-type: memif
    fpp-manager.cennso.com/plumbing-vni: "90"
  name: upg-inst01-7fc97c76bb-s5vq5-memif-client-90-j4fbw
  namespace: upg-fpp
spec:
  hardwareAddress: 02:67:03:0f:84:bf
  ifName: grx
  ips:
  - 198.18.0.20/24
  memif:
    id: 103
    role: slave
  mtu: 9000
  table: 100
status:
  memberCount: 0
  ready: true
  swIfIndex: 4
  vppName: grx
```

Interface name in VPP CLI and in upg metrics is set to `grx`.

Network Attachment definitions for grx/sx/sgi are added to upg deployment
by chart.

```yaml
  template:
    metadata:
      annotations:
        k8s.v1.cni.cncf.io/networks: upg-fpp-upg-inst01-grx@grx,upg-fpp-upg-inst01-sxb@sxb,upg-fpp-upg-inst01-sgi@sgi
```

`VPPInterface` is targeted against UPG deployment by label:
`fpp-manager.cennso.com/plumbing-instance-uid`.

### Additional interfaces

Any additional interfaces than grx/sgi/sx can be connected to UPG by creating
NAD and attaching its name in networks annotations. Chart configuration enables
to define extra NAD names in `additionalNetworks` value (per upg deployment)
or `instances.<instance-name>.networking.` (per UPG instance). NADs itself
should be provisioned outside of the chart.

```yaml
additionalNetworks:
  - upg-inst01-vpn@vpn
(...)
instances:
  inst01:
    networking:
      (...)
      additionalNetworks:
        - test-network-1-inst01
        - test-network-2-inst01
```

## VRF - Routing Table creation

UPG uses `RoutingTable` CR to handle routing table creation. VPP CLI commands
are not supported, as routing state is synced against CR objects, and additional
routes will be removed from VPP.

For UPG deployment, chart configuration will create appropriate `RoutingTable`
objects, for per interface route definition in `values.yaml`. Chart supports
routes for access network (grx), data network (sgi) and NAT pools (default gw).
Any additional routes should be defined during deployment, outside of UPG chart.

`RoutingTable` is targeted against UPG deployment by per instance label
`fpp-manager.cennso.com/plumbing-instance`, or by set of labels defined in
`VPPConfig` `spec.routingTableSelectors`. UPG charts generate list of label
based on chart name, release name and UPG instance name:

```yaml
  routingTableSelectors:
  - app.kubernetes.io/instance: upg-01-upg-fpp
    app.kubernetes.io/name: upg
    cennso.net/cnf: upg
    instance: inst01
```

More info on `RoutingTable` targeting is included in next paragraph.

Example of input of networking configuration:

```yaml
instances:
  inst01:
    networking:
      grx:
        (...)
        interface: grx
        ipv4:
          addresses:
           - 198.18.0.20/24
          routes:
            - to: 0.0.0.0/0
              via: 198.18.0.1
      sgi:
        (...)
        interface: sgi
        ipv4:
          addresses:
            - 172.22.11.5/24
          routes:
            - to: 1.2.3.4/24
              via: 172.22.11.1
    (...)
    nat:
      pools:
        pool-one:
          networking:
            (...)
            interface: one1
            ipv4:
              gw: 198.19.2.1
              addresses:
                - 198.19.2.21/24
```

This will create following `RoutingTable` CRs:

```yaml
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: RoutingTable
metadata:
  labels:
    app.kubernetes.io/instance: upg-01-upg-fpp
    app.kubernetes.io/name: upg
    cennso.net/cnf: upg
    instance: inst01
  name: upg-fpp-inst01-grx
  namespace: upg-fpp
spec:
  addressFamily: IPv4
  id: 100
  routes:
  - cidr: 0.0.0.0/0
    nextHopAddress: 198.18.0.1
    nextHopInterface: grx
---
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: RoutingTable
metadata:
  labels:
    app.kubernetes.io/instance: upg-01-upg-fpp
    app.kubernetes.io/name: upg
    cennso.net/cnf: upg
    instance: inst01
  name: upg-fpp-inst01-sgi
  namespace: upg-fpp
spec:
  addressFamily: IPv4
  id: 200
  routes:
  - cidr: 1.2.3.4/24
    nextHopAddress: 172.22.11.1
    nextHopInterface: sgi
---
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: RoutingTable
metadata:
  labels:
    app.kubernetes.io/instance: upg-01-upg-fpp
    app.kubernetes.io/name: upg
    cennso.net/cnf: upg
    instance: inst01
  name: upg-fpp-inst01-one1
  namespace: upg-fpp
spec:
  addressFamily: IPv4
  id: 300
  routes:
  - cidr: 0.0.0.0/0
    nextHopAddress: 198.19.2.1
    nextHopInterface: one1
```

To check routes applied on UPG instance, run VPP CLI:

```
vpp# sh ip fib
(...)
ipv4-VRF:100, fib_index:1, flow hash:[src dst sport dport proto flowlabel ] epoch:0 flags:none locks:[interface:2, API:1, adjacency:1, ]
0.0.0.0/0
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:9 buckets:1 uRPF:32 to:[0:0]]
    [0] [@3]: arp-ipv4: via 198.18.0.1 grx
(...)
198.18.0.0/24
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:16 buckets:1 uRPF:29 to:[0:0]]
    [0] [@4]: ipv4-glean: [src:198.18.0.0/24] grx: mtu:9000 next:1 flags:[] ffffffffffff0267037082450806
198.18.0.20/32
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:19 buckets:1 uRPF:22 to:[11:1320]]
    [0] [@12]: dpo-receive: 198.18.0.20 on grx
198.18.0.30/32
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:49 buckets:1 uRPF:72 to:[0:0]]
    [0] [@5]: ipv4 via 198.18.0.30 grx: mtu:9000 next:5 flags:[] 02fef38b03d60267037082450800
198.18.0.31/32
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:37 buckets:1 uRPF:52 to:[0:0] via:[11:1320]]
    [0] [@5]: ipv4 via 198.18.0.31 grx: mtu:9000 next:5 flags:[] 02fef38b03d60267037082450800
198.18.0.255/32
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:18 buckets:1 uRPF:20 to:[0:0]]
    [0] [@0]: dpo-drop ip4
(...)
```

### Targeting routing tables based on labels

To define routing table matching UPG instance, `routingTableSelectors` value
in chart `values.yaml` can be defined. It enables to apply one Routing table
to multiple UPG instance.

By definition of multiple label sets, one UPG instance can apply multiple
routing tables matching different labels, example configuration in chart
`values.yaml`:

```yaml
routingTableSelectors:
- crossInstance: default-routing-test-apn
```

This is propagated to `VPPConfig` that matches UPG instance:

```yaml
piVersion: fpp-manager.cennso.com/v1alpha1
kind: VPPConfig
metadata:
  labels:
    fpp-manager.cennso.com/plumbing-instance: upg-fpp-inst01
  name: upg-fpp-inst01
  namespace: upg-fpp
spec:
  (...)
  routingTableSelectors:
  - app.kubernetes.io/instance: upg-01-upg-fpp
    app.kubernetes.io/name: upg
    cennso.net/cnf: upg
    instance: inst01
  - crossInstance: default-routing-test-apn
```

UPG chart will generate the basic routing tables matching
`app.kubernetes.io/instance`, `app.kubernetes.io/name`, `cennso.net/cnf`
and `instance` labels. There can be created additional routing table objects,
that match only additional label (`crossInstance` in example) outside of UPG
chart:

```yaml
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: RoutingTable
metadata:
  labels:
    crossInstance: default-routing-test-apn
  name: default-inet
  namespace: upg-fpp
spec:
  addressFamily: IPv4
  id: 201
  routes:
  - cidr: 0.0.0.0/0
    nextHopAddress: 172.20.1.1
    nextHopInterface: sgi
```

In that case, all UPG instance that have VPPConfig with `crossInstance` label
defined will fetch and apply above routing table.

## NWI creation
In PGW mode, 3 NWIs should be created: `epc`, `cp` and `sgi`. For TDF mode,
NWIs: `access`, `cp` and multiple `sgi`s (one per destination, nat pool) should
be created.

UPG chart takes care of proper NWI creation, based on `values.yaml` `type` value:
`pgw` or `tdf`. One `sgi` per nat pool is added after declaration `nat.enabled: true`
and `nat.type` set to `cgnat`.

### NWI CR config
UPG uses `UPGConfig` CR to create NWIs. Old way of CLI NWI creation is not
supported, as the only source of truth is Kubernetes API CRs.

For `pgw` UPG type, following NWIs will be created by chart:

```yaml
apiVersion: upg-manager.cennso.com/v1alpha1
kind: UPGConfig
metadata:
  labels:
    fpp-manager.cennso.com/plumbing-instance: upg-fpp-inst01
  name: upg-fpp-inst01
  namespace: upg-fpp
spec:
  nwis:
  - ipv4TableID: 0
    ipv6TableID: 0
    name: cp
  - ipv4TableID: 100
    ipv6TableID: 100
    name: epc
  - ipv4TableID: 200
    ipv6TableID: 200
    name: sgi
```

For `tdf` type `values.yaml`:

```yaml
type: tdf

nat:
  enabled: true
  type: cgnat
  maxsessions: 500000
instances:
  inst01:
    networking:
      (...)
    nat:
      pools:
        pool-de:
          networking:
            interface: de1
        (...)
        pool-at:
          networking:
            interface: at1
        (...)
```

And resulting `UPGConfig`:

```yaml
apiVersion: upg-manager.cennso.com/v1alpha1
kind: UPGConfig
metadata:
  labels:
    fpp-manager.cennso.com/plumbing-instance: upg-fpp-inst01
  name: upg-fpp-inst01
  namespace: upg-fpp
spec:
  nwis:
  - ipv4TableID: 0
    ipv6TableID: 0
    name: cp
  - ipv4TableID: 100
    ipv6TableID: 100
    name: access
  - ipv4TableID: 200
    ipv6TableID: 200
    name: at1
  - ipv4TableID: 300
    ipv6TableID: 300
    name: de1
```

To check the NWI configuration on running UPG instance, use VPP CLI:

```
vpp# sh upf nwi
epc, ip4-table-id 100, ip6-table-id 100, ipfix-policy none, ipfix-collector-ip 0.0.0.0
sgi, ip4-table-id 200, ip6-table-id 200, ipfix-policy none, ipfix-collector-ip 0.0.0.0
cp, ip4-table-id 0, ip6-table-id 0, ipfix-policy none, ipfix-collector-ip 0.0.0.0
```

## Features

### Policy-based routing configuration

Policy-based routing, or traffic steering feature, provides possibility to
apply routing rules per session and forward traffic to different data networks
like Internet or VPN. This feature assumes SMC support in PFCP Session Creation
Request using 3GPP TS 29.244 V16.9.1 8.2.23 Forwarding Policy IE.

During session establishment, SMC assigns a policy by adding the Forwarding
Policy IE to the FAR(s) with Destination Interface SGi-LAN/N6-LAN.
For each session, one of the policies is applied, causing a lookup in one of
the respective tables. UPG and SMC configuration should be in sync, SMC should
send already configured policy IDs in UPG config. Policy ID is a regular string
with max 63 characters. For example `defaultInet` or `defaultVpn`.

Example topology overview:

![policy based routing topology](https://docs.cennso.com/api/assets/components/upg/2.2/images/policy-based-topology.png)

Steps needed to configure policy based routing:
1. Regular SGI interface (called `inet` in above picture) can be used for
Internet connectivity, chart will create NAD for `inet`
1. Ensure that VXLANs for SGI and VPN interface are created (outside of UPG
chart), to enable connectivity to Internet and VPN CGWs - both gateways need
to have FPP-CNI `tap` interfaces deployed
1. Create NAD for VPN interface (outside of UPG chart)
1. Define additional interface in UPG deployment for VPN connectivity -
`additionalNetworks` in `instances.<name>.networking`
1. Define policy names in UPG config using `additionalConfig` section in
`instances.<name>`
1. Define routing tables attached to policy created, outside of UPG chart
1. For multi UPG instances, define `routingTableSelectors` so that one routing
table object will be used on each UPG.

Please refer to [policy based routing values.yaml](https://docs.cennso.com/api/assets/components/upg/2.2/example-config/pb-routing-values.yaml)
and example of [NAD and RoutingTable](https://docs.cennso.com/api/assets/components/upg/2.2/example-config/pb-additional-config.yaml)
definitions for full configuration needed.

Policy based routing feature should be used in `pgw` mode of UPG. To define
forwarding policy, CLI commands in `instances.<name>.additionalConfig` should
be used, example:

```yaml
instances:
  inst01:
    (...)
    additionalConfig:
      - ip table add 201
      - ip table add 202
      - ip6 table add 201
      - ip6 table add 202
      - upf policy add id defaultInet via ip4-lookup-in-table 201 via ip6-lookup-in-table 201
      - upf policy add id defaultVpn via ip4-lookup-in-table 202 via ip6-lookup-in-table 202
      - upf policy add id defaultInetIPFix via ip4-lookup-in-table 201 via ip6-lookup-in-table 201
      - upf policy add id defaultVpnIPFix via ip4-lookup-in-table 202 via ip6-lookup-in-table 202
```

Current requirement is to create tables first before `upf policy add id <name>`
command is used. Table creation in `additionalConfig` should be removed after
implementing conditional wait for RoutingTable CR to be added before executing
`additionalConfig` commands.

`upf policy add id <policy-name> via ip4-lookup-in-table <table-id> via ip6-lookup-in-table <table-id>`
defines `<policy-name>` to be used for traffic forwarding, with connected IPv4
and IPv6 routing table look ups. Routing table must exist before referencing
them in policy creation command. If routing table is deleted during runtime,
policy is working, but no traffic is forwarded outside of UPG. After routing
table is available again, traffic is passing normally.

Above example of policy creation shows how to configure policy based routing
together with IPFIX activation per session. Two policies needs to be created,
for example `defaultInet` and `defaultInetIPFix`, and proper configuration at
SMC side will send PFCP IEs for routing (FAR - forwarding policy name) and
ipfix template (TP IPFIX Policy IE - `dest` as prepared for PGW mode).

IPFIX configuration includes exporter config:

```yaml
ipfixExporters:
  - collectorAddress: 172.18.0.196
    collectorPort: 4739
    pathMTU: 1420
    srcNetwork: sx
    templateInterval: 5
    udpChecksum: false
    exporterID: "pool1"
```

And SGI IPFIX configuration:

```yaml
instances:
  inst01:
    networking:
      (...)
      sgi:
        interface: inet
        type: memif
        vni: 2130
        mtu: 8930
        ipv4:
          addresses:
          - 172.20.0.16/24
        ipv6:
          addresses:
          - fd71:357a:a82e:1::16/64
        ipfixCollector: 172.18.0.196
        observationDomainID: 21
        observationDomainName: pgw.upg-01
        observationPointID: 1
        ipfixReportInterval: 10
```

For VPN interface, separate NAD needs to be created outside of UPG chart.
Example:

```yaml
apiVersion: k8s.cni.cncf.io/v1
kind: NetworkAttachmentDefinition
metadata:
  name: vpn21-memif-inst01
  namespace: proxy-load-cennso-net
spec:
  config: |-
    {
      "cniVersion": "0.3.1",
      "ipam": {
        "addresses": [
          {
            "address": "172.21.0.16/24"
          },
          {
            "address": "fd71:357a:a831:1::16/64"
          }
        ],
        "type": "static"
      },
      "mtu": 8930,
      "name": "vpn21",
      "table": 200,
      "type": "fpp-cni",
      "vni": 2150,
      "vppIfaceType": "memif"
    }
```

This NAD `vpn21-memif-inst01` can be added in `additionalNetworks` in per
instance network configuration:

```yaml
instances:
  inst01:
    networking:
      grx: (...)
      sx: (...)
      sgi: (...)
      additionalNetworks:
        - vpn21-memif-inst01@vpn21
```

Last step is to configure routing tables connected to policy tables. This needs
to be done outside of UPG chart. Policy based routing enables pointing default
route to Internet and specific IP network addresses to VPN data network. Also
blocking of IP range is allowed. Example:

```yaml
---
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: RoutingTable
metadata:
  labels:
    fpp-manager.cennso.com/plumbing-instance: pcg-proxy-load-cennso-net-upg
  name: default-inet
  namespace: proxy-load-cennso-net
spec:
  addressFamily: IPv4
  id: 201
  routes:
  - cidr: 0.0.0.0/0
    nextHopAddress: 172.20.0.96
    nextHopInterface: inet
  - cidr: 172.21.0.128/32
    nextHopAddress: 172.21.0.128
    nextHopInterface: vpn21
  - cidr: 9.9.9.9/32
    nextHopAddress: blackhole
---
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: RoutingTable
metadata:
  labels:
    fpp-manager.cennso.com/plumbing-instance: pcg-proxy-load-cennso-net-upg
  name: default-vpn
  namespace: proxy-load-cennso-net
spec:
  addressFamily: IPv4
  id: 202
  routes:
  - cidr: 0.0.0.0/0
    nextHopAddress: 172.21.0.128
    nextHopInterface: vpn21
  - cidr: 198.19.1.0/24
    nextHopAddress: 172.20.0.96
    nextHopInterface: inet
  - cidr: 8.8.8.8/32
    nextHopAddress: 172.20.0.96
    nextHopInterface: inet
  - cidr: 8.8.4.4/32
    nextHopAddress: 172.20.0.96
    nextHopInterface: inet
  - cidr: 9.9.9.9/32
    nextHopAddress: blackhole
```

Above example shows different routes configuration for tables 201 and 202,
where 201 table pass default traffic to Internet, and specific address to VPN
data network. Table 202 does opposite routing, by default passing traffic to
VPN data network, and only specific routes are forwarded to SGI (Internet)
network. IP `9.9.9.9/32` is blocked.

Common label across all RoutingTable CRs is configured in UPG chart by:

```yaml
routingTableSelectors:
- fpp-manager.cennso.com/plumbing-instance: pcg-proxy-load-cennso-net-upg
```

### IPFIX configuration

UPG enables IPFIX to be exported. This is built in feature and to properly
use it, IPFIX exporter and per NWI IPFIX settings should be configured. IPFIX
exporter configures the IPFIX collector, these are current assumptions for
IPFIX exporter configuration:
- UPG supports sending IPFIX data over VPP interface, so current configuration
does not support to use Kubernetes default network (like Calico)
- Collector IP address should use FPP-CNI, and recommended is control network
(`sx`), as it was tested and reported to be working
- IPFIX collectors are distinguish by IP address, so only one collector per
IP is supported currently (this is VPP internal limitation)
- Collector IP defined in `ipfixExportes` should match per NWI `ipfixCollector`
settings, otherwise IPFIX feature will not work

Example:

```yaml
ipfixExporters:
  - exporterID: data-net-exporter
    collectorAddress: 172.18.1.100
    collectorPort: 4739
    pathMTU: 1420
    srcNetwork: sx
    templateInterval: 15
    udpChecksum: false
```

UPG supports two IPFIX templates:
- `default` - mainly created for TDF use-case, with NAT information
- `dest` - used in PGW mode

Second part of IPFIX configuration is per NWI ipfix config, supported in `sgi`
or per NAT pool configuration in `values.yaml`, example for PGW mode:

```yaml
instances:
  inst01:
    networking:
      grx: (...)
      sx: (...)
      sgi:
        interface: sgi21
        type: memif
        vni: 2130
        mtu: 8930
        ipv4:
          addresses:
          - 172.20.0.16/24
        ipv6:
          addresses:
          - fd71:357a:a82e:1::16/64
        ipfixCollector: 172.18.1.100
        # ipfixPolicy: dest
        observationDomainID: 21
        observationDomainName: pgw.sgi-example
        observationPointID: 1
        ipfixReportInterval: 10

```

`ipfixExporters.collectorAddress` and `instances.<name>.networking.sgi.ipfixCollector`
needs to match, otherwise IPFIX will not be sent. For PGW mode, `ipfixPolicy`
can be set in NWI configuration, or if policy is omitted, PFCP Session
Establishment Request should include PFCP TP IPFIX Policy IE with a valid name
of the policy (`dest`/`default`). PGW mode can use `dest` as `ipfixPolicy`. In
case where `ipfixPolicy` is empty, no ipfix is sent by default and IPFIX is
activated per session.

For TDF mode, per NAT pool ipfix configuration is supported, and should be
included in NAT config section, example:

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

To check IPFIX configuration, VPP CLI on UPG can be executed:

```
vpp# sh upf nwi
cp, ip4-table-id 0, ip6-table-id 0, ipfix-policy none, ipfix-collector-ip 0.0.0.0
access, ip4-table-id 100, ip6-table-id 100, ipfix-policy none, ipfix-collector-ip 0.0.0.0
at1, ip4-table-id 200, ip6-table-id 200, ipfix-policy default, ipfix-collector-ip 172.18.1.101
de1, ip4-table-id 300, ip6-table-id 300, ipfix-policy default, ipfix-collector-ip 172.18.1.100
```

### TDF with CG-NAT
To configure UPG in TDF mode, following settings needs to be defined in
`values.yaml`.

First of all, we need to define `type` to `tdf`.

As UPG require FPP-enabled node to run on, `nodeAffinity` needs to be defined.
For example if all nodes with fpp enabled are labeled with
`fpp-manager.cennso.com/vppswitch: ""`, then chart config should include:

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

If UPG deployment should be pinned to specific node, then affinity can look
like this (assuming node name is `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
```

`labels` needs to be defined, because there are used in `upg-support` Grafana
dashboard:

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

If UPG Manager image is hosted on private registry, `imagePullSecrets` should be
set in `values.yaml`, example:

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

This will set the service account to use that secret, as well as it will be
set in UPG deployment. The secret needs to be created outside UPG deployment.

Based on load test (250k sessions), recommendation for resource allocation is
following:

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

`enableHugepagesAllocation` defines if UPG should use hugepages for heap, stats
segment and buffer. If set to `false`, UPG will not use hugepages extensively.
If set to `true` - `hugepages-2Mi` should be set to `heapsize` plus 2G.
It was tested, that enable/disable hugepages allocation for heap, stats and
buffer does not influence performance or stability of UPG.

Networking configuration for UPG instance includes regular `grx` and `sx`
interface configuration. `sgi` configuration is omitted, as per pool `sgi`
will be added. Example config:

```yaml
instances:
  inst01:
    networking:
      # access network
      grx:
        interface: an1
        type: memif
        vni: 4201
        ipv4:
          addresses:
            - 198.18.1.21/24
          routes:
            # 10.0.0.0/10 is the UE address range
            - to: 10.0.0.0/10
            # via is a IP of access network gateway
              via: 198.18.1.100
      # control network
      sx:
        interface: sx1
        type: memif
        vni: 4101
        mtu: 8930
        ipv4:
          addresses:
            -  172.18.1.21/24
```

Above will cause upg chart to define NADs with memif interfaces for grx (named
`an1`) and sx (named `sx1`). Proper VXLANs should be created (i.e. 4201 and
4101) using Custom Resource of `vxlan.fpp-manager.cennso.com`. UPG chart does
not created those VXLAN objects. This will enable cross node communication.
Example:

```yaml
---
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: VXLAN
metadata:
  name: an1
  namespace: tdf-example
spec:
  vni: 4201
---
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: VXLAN
metadata:
  name: sx1
  namespace: tdf-example
spec:
  vni: 4101

```

Usually TDF setup requires NAT. UPG enables 2 types of NAT: basic and carrier
grade NAT (CG-NAT). In this case, `cg-nat` will be used. To enable `cg-nat`
`values.conf` should include:

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

Where `500000` is a number of connections for all users, and this number was
verified by load test to be sufficient (250k sessions run).

NAT pools are configured in `instances.<name>.nat` section of chart `values.yaml`.
Example NAT pool configuration:

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

In per pool configuration, ipfix capabilities are defined, interface details
(name, type, mtu, IP address and pool gateway address). `vni` refers to FPP
VXLAN network id. This vxlan needs to be created with with
`vxlan.fpp-manager.cennso.com` CRs outside of UPG chart. Above configuration
will generate NADs for NAT pool interfaces, with `fpp-cni` and `memif` type.

```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",
      "vni": 4303,
      "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",
      "vni": 4302,
      "vppIfaceType": "memif"
    }
```

Based on NAT pool definitions, NWI per NAT pool will be defined using
`UPGConfig` CR. Also IPFIX configuration per NWI will be included.
IPFIX template `default` should be used for TDF use-case:

```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: default
    observationDomainID: 21
    observationDomainName: tdf.at-inst01
    observationPointID: 1
    ipfixReportInterval: 10
    ipv4TableID: 200
    ipv6TableID: 200
    name: at1
  - ipfixCollectorAddress: 172.18.1.100
    ipfixPolicy: default
    observationDomainID: 21
    observationDomainName: tdf.de-inst01
    observationPointID: 1
    ipfixReportInterval: 10
    ipv4TableID: 300
    ipv6TableID: 300
    name: de1
```

NAT pools configuration will be 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 }}
  - nat44 enable sessions 500000
  - upf nat pool 198.19.16.1 - 198.19.16.254 block_size 512 nwi at1 name pool-at
  - set interface nat44 out {{ (index .Ifs "at1").Name }} output-feature
  - 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
  - set interface nat44 out {{ (index .Ifs "de1").Name }} output-feature
  - upf ueip pool nwi de1 id pool-de
  - nat44 controlled enable
  - 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
```

This is example exporters configuration:

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

### Rate limiter

For TDF case, there is possibility to throttle the session creation rate, which
is important to not overwhelm SMF. Rate limiter will be applied to PFCP Session
Report Request, sent from UPG to SMF. `rateLimiterPPS` included in `values.yaml`
will set how many messages per second can be sent.

```yaml
rateLimiterPPS: 150
```

## PFCP Server Configuration

`UPGConfig` CR can be used to configure UPG PFCP Server parameters.

### PFCP Heartbeat Configuration

PFCP Heartbeat mechanism is used to identify connectivity problems between UPG and
CP-Function. The number of retries for unanswered PFCP Heartbeat Request and the
timeout between retries in seconds can be configured within `pfcp` section of
`UPGConfig`.

```yaml
apiVersion: upg-manager.cennso.com/v1alpha1
kind: UPGConfig
metadata:
  labels:
    fpp-manager.cennso.com/plumbing-instance: upg-fpp-inst01
  name: upg-fpp-inst01
  namespace: upg-fpp
spec:
  pfcp:
    heartbeat:
      retries: 10
      timeout: 3
  nwis:
...
```

`UPGConfig` CR is populated with dedicated values by setting appropriate parameters
in `values.yaml`. Note, that number of retires should not be greater than `30`, and
timeout should be positive number in seconds not greater than `120`.

```yaml
pfcpHeartbeatTimeout: 10
pfcpHeartbeatRetries: 5
```

PFCP Heartbeat configuration can be verified from VPP CLI:

```
vppctl# show upf heartbeat-config
Timeout: 10 Retries: 5
```
