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

# Deploy Fast Path Provider for CGW 2 with basic NAT

This document describes the prerequisites and steps to deploy the Fast Path Provider (FPP) for the Connectivity Gateway (CGW) 2 with NAT use case.

## Prerequisites

- [Requirements](https://docs.cennso.com/components/fpp/3.2/requirements.md)
- [VPP Operator deployed](https://docs.cennso.com/components/fpp/3.2/deployment-guides/vppoperator-deployment.md)

## Steps

1. Deploy FPP for CGW 2 with basic NAT using the following configuration:

   Connectivity Gateway (CGW) using VPP as a packet processing engine has better performance than a Linux kernel-based solution. For cluster-internal usage, CGW can handle routing and apply basic NAT.

   >**CAUTION:** FPP-based CGWs must be only scheduled on the nodes with FPP version 2.0.0 or later serving as VPP Switch to have VPP interfaces configured properly.

   Below is a diagram with the CGW with NAT example deployment described further in this section:

   ![CGW2 with NAT](https://docs.cennso.com/api/assets/components/fpp/3.2/assets/cgw2-withnat.svg)

   This is the full configuration of the CGW deployment:

   ```yaml
   # Apply this on the Management Cluster
   apiVersion: fpp-manager.cennso.com/v1alpha1
   kind: CGWDeployment
   metadata:
     name: example-cgw-nat
   spec:
     connectionSecretRef:
       name: example-core-cluster
       namespace: cennso-infra
     targetNamespace: fpp
     cnfName: cgw
     operatorName: node-op

     imagePullSecrets:
     - name: image-secret

     vppConfigs:
     - spec:
         interfaces:
         - inet
         afterCommands:
         - set ip neighbor-config ip4 age 10
         # Set number of worker handoff frame queue elements.
         # 4096 if 2 memifs with 2 worker threads are used
         # 10240 if memif plus DPDK interface is used with 2 worker threads
         # - set nat frame-queue-nelts 4096
         # Defined the number of NAT translations, preallocated after NAT plugin starts
         - nat44 plugin enable sessions 1000000
         - set interface nat44 out {{ (index .Ifs "inet").Name }} output-feature
         - nat44 add address 10.10.0.120 - 10.10.0.254
         - set nat timeout udp 300 tcp-established 7440 tcp-transitory 240 icmp 60
         - nat mss-clamping 1300

     networks:
       # network with NATed access to the internet
       inet:
         networkName: net-inet
         tableName: rt-inet
         # number of Queues adjust to worker threads
         # numRxQueues: 2
         # numTxQueues: 2
         mtu: 1464
         ipv4:
           addresses:
           - 10.10.0.1/24
       # network without NAT to reach the backend services
       vpn:
         networkName: net-vpn
         tableName: rt-inet
         # number of Queues adjust to worker threads
         # numRxQueues: 2
         # numTxQueues: 2
         mtu: 1464
         ipv4:
           addresses:
           - 10.11.0.1/24
       # IP traffic coming to/from UPG (UEs traffic for example)
       sgi:
         networkName: net-sgi
         tableName: rt-sgi
         # number of Queues adjust to worker threads
         # numRxQueues: 2
         # numTxQueues: 2
         mtu: 1500
         ipv4:
           addresses:
           - 198.19.0.1/24
         ipv6:
           addresses:
           - fd71:357a:a82e:1::96/64

     routingTables:
       rt-inet:
         id: 10
         addressFamily: IPv4
         routes:
         - cidr: 198.19.0.0/24
           nextHopInterface: sgi
       rt-sgi:
         id: 20
         addressFamily: IPv4
         routes:
         - cidr: 10.10.0.0/24
           nextHopInterface: inet
         - cidr: 10.11.0.0/24
           nextHopInterface: vpn

     # Minimal resource configuration that enables CGW to run:
     resources:
        cpu: "2"
        memory: 2Gi
        hugepages-2Mi: 1Gi

     heapSize: 1900M
     useHugepages: false
     buffersPerNuma: 256000

     # optional log level extension to more verbose mode:
     # vpp:
     #   traceAPICalls: true
     # logLevel: "trace"
   ```

2. Configure the CGW deployment based on the example above.

   - Configure the VPP Operator that should handle the FPP deployment creation:

   ```yaml
   operatorName: node-op
   ```

   - Provide the CNF name:

   ```yaml
   cnfName: cgw
   ```

   The configuration above creates the VPPDeployment object with mode set to `instance`, which in turn creates Kubernetes `StatefulSet` object (as opposed to `DaemonSet` which is used for the `node` mode), and adds the `cennso.net/cnf: cgw` label to all created resources.

   - Define **imagePullSecrets** to download the VPP Worker image from a private registry:

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

   - Configure the networking.

   This example uses memifs for in-cluster networking. Starting from the left side, **sgi** interface is created. This connects the IP network receiving traffic from UPG. Next, on the right side, you have two network interfaces. One is **inet**, which has connectivity to the Internet. Second is **vpn**, which connects to the backend services:

   ```yaml
   networks:
     # network with NATed access to the internet
     inet:
       networkName: net-inet
       tableName: rt-inet
       # number of Queues adjust to worker threads:
       # - 1 (default) for CPU 2
       # - 2 - if CPU set to 6
       # numRxQueues: 2
       # numTxQueues: 2
       mtu: 1464
       ipv4:
         addresses:
         - 10.10.0.1/24
     # network without NAT to reach the backend services
     vpn:
       networkName: net-vpn
       tableName: rt-inet
       # numRxQueues: 2
       # numTxQueues: 2
       mtu: 1464
       ipv4:
         addresses:
         - 10.11.0.1/24
     # IP traffic coming to/from UPG (UEs traffic for example)
     sgi:
       networkName: net-sgi
       tableName: rt-sgi
       # numRxQueues: 2
       # numTxQueues: 2
       mtu: 1500
       ipv4:
         addresses:
         - 198.19.0.1/24
   ```

   - The routing tables are configured as follows:

   ```yaml
   routingTables:
     rt-inet:
       id: 10
       addressFamily: IPv4
       routes:
       - cidr: 198.19.0.0/24
         nextHopInterface: sgi
     rt-sgi:
       id: 20
       addressFamily: IPv4
       routes:
       - cidr: 10.10.0.0/24
         nextHopInterface: inet
       - cidr: 10.11.0.0/24
         nextHopInterface: vpn
   ```

   Based on the routing applied in table **rt-sgi**, the incoming traffic is forwarded either to the **inet** interface, where the NAT is applied, or routed to the **vpn** interface, which connects user traffic with the corporate backend (no NAT applied). **rt-inet** has the backroute information on where to forward the returning traffic.

   - Configure the NAT using VPP CLI

   FPP enables configuring the basic NAT using CLI commands in `vppConfigs`:

   ```yaml
   vppConfigs:
   - spec:
       interfaces:
       - inet
       afterCommands:
       - set ip neighbor-config ip4 age 10
       # Set number of worker handoff frame queue elements.
       # - for 1 worker thread - not set
       # - for 2 worker threads and 2 memifs - 4096 - sum of interfaces queue size multiplied by 4
       # - for 2 worker threads and memifs plus DPDK - 10240
       - set nat frame-queue-nelts 4096
       # Defined the number of NAT translations, preallocated after NAT plugin starts
       - nat44 plugin enable sessions 1000000
       # Configure the NAT on output of inet interface, it works post routing
       - set interface nat44 out {{ (index .Ifs "inet").Name }} output-feature
       # Add NAT addresses, it can be inet interface IP or defined IP range.
       - nat44 add address 10.10.0.120 - 10.10.0.254
       # The NAT timeout can be configured, below are the default values
       - set nat timeout udp 300 tcp-established 7440 tcp-transitory 240 icmp 60
       # The TCP MSS adjustment is supported in the NAT
       - nat mss-clamping 1300
   ```

   The `frame-queue-nelts` parameter is recommended to be set if we use more than 1 worker thread. The calculation uses the following formula:

   ```
   (sum of interfaces queue size) * 4
   ```

   - queue size for memif is 512
   - DPDK queue size is configurable (using **numRxDesc** and **numTxDesc**), by default set to 2048 each
   - When using 2 memifs for NAT input and NAT output interface - this gives (512+512)*4 = `4096`
   - When using memif and DPDK interface - (512+2048)*4 = `10240`

   For more configuration options, [visit the official VPP NAT documentation](https://s3-docs.fd.io/vpp/24.02/developer/plugins/nat44_ed_doc.html).

   - Configure the resources

   The minimal resource configuration to achieve 500k NAT translations in a production environment is as follows:

   ```yaml
   resources:
      cpu: "2"
      memory: 5Gi
      hugepages-2Mi: 1Gi

   heapSize: 512M
   useHugepages: false
   buffersPerNuma: 256000
   ```

   > [!CAUTION]
   > Do not run a CGW with NAT on exactly one VPP worker thread, because the built-in `vppctl ping` command then crashes VPP. The examples on this page assume a node with Hyper-Threading (HT), where each physical core has two logical CPUs. FPP uses one logical CPU of each physical core. VPP runs the main thread on the first of these CPUs and a worker thread on each of the others. With `cpu: "4"`, VPP runs one main thread and exactly one worker thread. Set **cpu** to `2`, or to `6` or more. If the CGW already runs with one worker thread, do not run `vppctl ping`. Check connectivity with ping from a host outside the CGW.

   For a use case that requires higher performance, assign 6 CPU cores.

   Measured memory consumption for different settings:

   | Cores | NAT translations | NAT address range | Heap | MainMemory | Resources recommendation |
   |---|---|---|---|---|---|
   | 2 | 500 000 | /24 | 287 MiB | 4.15 GiB | memory: 5Gi, heapSize: 512M |
   | 2 | 1 000 000 | /24 | 498 MiB | 4.25 GiB | memory: 5Gi, heapSize: 1G |
   | 6 | 1 000 000 | /23 | 1.15 GiB | 5.01 GiB | memory: 6Gi, heapSize: 2G |
   | 6 | 2 000 000 | /23 | 2,16 GiB | 6.03 GiB | memory: 7Gi, heapSize: 3G |
   | 6 | 4 000 000 | /23 | 4.2 GiB | 8.07 GiB | memory: 9Gi, heapSize: 5G |
