> 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/operation-guides/policy-based-routing.md).

# Configure policy-based routing

Policy-based routing, also known as traffic steering, allows you to set routing rules for each session and forward traffic to different Data Networks, such as the Internet or a VPN. This feature requires [Session Management Controller](https://docs.cennso.com/components/smc/main.md) (SMC) support in the PFCP Session Creation Request, using the Forwarding Policy IE (defined in 3GPP TS 29.244 V16.9.1 8.2.23).

Policy-based routing is supported only in the PGW use case. During session setup, SMC assigns a policy by adding the Forwarding Policy IE to the FAR(s) with the SGi-LAN or N6-LAN destination interface. For each session, one policy is applied, and traffic is forwarded based on a lookup in the relevant routing table. The UPG and SMC configurations must match, and SMC should send the correct policy IDs from the UPG configuration. A policy ID is a string (max 63 characters), such as `defaultInet` or `defaultVpn`.

![Policy-based routing topology](https://docs.cennso.com/api/assets/components/upg/3.2/assets/policy-based-routing.svg)

## Configuration

To configure policy-based routing:

1. Create FPPNetworks for the SGi and VPN interfaces in the Core Cluster, independent from the UPGDeployment CR, which is applied to the Management Cluster. This enables cross-node connectivity to CGW Internet and CGW VPN. For the purpose of this tutorial, let's call them `sgi-net` and `vpn-net`. See the example:

   ```yaml
   apiVersion: fpp-manager.cennso.com/v1alpha1
   kind: FPPNetwork
   metadata:
     name: sgi-net
   spec:
     type: vxlan
     id: 2142
   ---
   apiVersion: fpp-manager.cennso.com/v1alpha1
   kind: FPPNetwork
   metadata:
     name: vpn-net
   spec:
     type: vxlan
     id: 2144
   ```

2. [Configure the SGi interface](https://docs.cennso.com/components/upg/3.2/operation-guides/interface.md#create-interfaces-using-upgdeployment-cr). The UPGDeployment CR creates a VPPInterface object for the SGi interface (like `inet` in the diagram), which is used for Internet connectivity. Sample configuration:

   ```yaml
   spec:
     operatorName: upg-operator
     labels:
       apn: proxy.load.cennso.net

     instances:
       inst01:
         networking:
           (...)
           sgi:
             interface: inet
             networkName: sgi-net
             mtu: 8930
             ipv4:
               addresses:
               - 172.20.0.16/24
             ipv6:
               addresses:
               - fd71:357a:a82e:1::16/64
   ```

   The resulting VPPInterface for the **upg-pbr** UPG deployment looks like this:

   ```yaml
   apiVersion: fpp-manager.cennso.com/v1alpha1
   kind: VPPInterface
   metadata:
     name: upg-pbr-inst01-inet
     labels:
       apn: proxy.load.cennso.net
       helm.sh/chart: upg-3.0.0
       app.kubernetes.io/managed-by: Helm
       app.kubernetes.io/version: 3.0.0
       app.kubernetes.io/name: upg
       app.kubernetes.io/instance: upg-pbr
       fpp-manager.cennso.com/created-for: upg-operator
       release: upg-pbr
       cennso.net/cnf: upg
       fpp-manager.cennso.com/plumbing-instance: upg-pbr-inst01
       fpp-manager.cennso.com/plumbing-ifname: "inet"
   spec:
     ifName: inet
     attachToNetwork: sgi-net
     table: 200
     memif:
       role: slave
     ips:
     - 172.20.0.16/24
     - fd71:357a:a82e:1::16/64
     mtu: 8930
   ```

3. Create a separate VPPInterface for the VPN interface in the Core Cluster outside of the UPGDeployment CR:

   ```yaml
   apiVersion: fpp-manager.cennso.com/v1alpha1
     kind: VPPInterface
     metadata:
       labels:
         apn: proxy.load.cennso.net
         cennso.net/cnf: upg
         fpp-manager.cennso.com/created-for: upg-operator
         fpp-manager.cennso.com/plumbing-ifname: vpn
         fpp-manager.cennso.com/plumbing-instance: upg-pbr-inst01
       name: upg-inst01-vpn
     spec:
       attachToNetwork: vpn-net
       ifName: vpn
       ips:
       - 172.21.0.16/24
       - fd71:357a:a831:1::16/64
       memif:
         role: slave
       mtu: 8930
       table: 200
   ```

   The configuration assigns IPs to UPG's VPN memif interface, which connects to the `vpn-net` FPPNetwork. This interface uses table `200`, which is the default routing table created by the UPGDeployment CR.

4. [Configure IPFIX](https://docs.cennso.com/components/upg/3.2/operation-guides/ipfix.md#configuration) to allow for sending IPFIX data over a VPP interface. First, configure IPFIX exporter:

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

   Then, configure IPFIX for the SGi network in the **spec.instances.{NAME}.networking** section. See the example:

   ```yaml
   spec:
     instances:
       inst01:
         networking:
           (...)
           sgi:
           (...)
             ipfixCollector: 172.18.0.196
             observationDomainID: 21
             observationDomainName: pgw.upg-01
             observationPointID: 1
             ipfixReportInterval: 10
   ```

5. Define Forwarding Policies in the UPG configuration under **spec.instances.{name}.forwardingPolicies**. This creates UPGForwardingPolicy objects on the Core Cluster. Add the required table IDs in **spec.instances.{name}.requiredRoutingTables** to create empty IPv4 and IPv6 tables. If a routing table is deleted at runtime, the policy remains active, but traffic won’t be forwarded outside UPG until the table is restored.

   ```yaml
   spec:
     instances:
       inst01:
         (...)
          forwardingPolicies:
          - defaultInet:
              ipv4TableID: 201
              ipv6TableID: 201
          - defaultInetIPFix:
              ipv4TableID: 201
              ipv6TableID: 201
          - defaultVpn:
              ipv4TableID: 202
              ipv6TableID: 202
          - defaultVpnIPFix:
              ipv4TableID: 202
              ipv6TableID: 202
         requiredRoutingTables:
           - 201
           - 202
   ```

   This example configures policy-based routing with IPFIX activation per session. Different policies are defined, such as `defaultInet` and `defaultInetIPFix`. This allows SMC to decide whether to track traffic with IPFIX for each session or not. During PFCP session establishment, SMC selects one of the configured policies by name (for example, `defaultInet` or `defaultInetIPFix`) and includes it in the PFCP Session Establishment Request. The request contains the routing information in the FAR (Forwarding Action Rule), along with the selected IPFIX policy name provided in the TP IPFIX Policy IE (FlowUsage).

   > [!NOTE]
   > If you are modifying an existing UPGDeployment, you must recreate the UPG pod to apply the updated VPP CLI configuration. See [Reconfiguration at runtime](https://docs.cennso.com/components/upg/3.2/troubleshooting/reconfig.md) for details.

6. Define routing tables for the policies in a separate YAML file using the `RoutingTable` CR object. Configure routing tables connected to the policy tables in the Core Cluster, outside the `UPGDeployment` CR. Policy-based routing allows you to point the default route to the Internet and specific IP addresses to the VPN data network. You can also block specific IP ranges. Here's an example of routing table definitions:

   ```yaml
   ---
   apiVersion: fpp-manager.cennso.com/v1alpha1
   kind: RoutingTable
   metadata:
     labels:
       fpp-manager.cennso.com/created-for: upg-operator
       fpp-manager.cennso.com/plumbing-instance: upg-pbr-selector
     name: default-inet
     namespace: basic-pgw
   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/created-for: upg-operator
       fpp-manager.cennso.com/plumbing-instance: upg-pbr-selector
     name: default-vpn
     namespace: basic-pgw
   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
   ```

   The example shows different routes for tables `201` and `202`. Table `201` routes default traffic to the Internet and specific addresses to the VPN data network. Table `202` does the opposite, sending default traffic to the VPN data network and forwarding specific routes to the SGi (Internet) network. IP `9.9.9.9/32` is blocked for both routes.

7. For multiple UPG instances, define **routingTableSelectors** so that the same routing table object is applied to each UPG. The common label for all `RoutingTable` CRs is configured in the `UPGDeployment` CR as follows:

   ```yaml
   spec:
     routingTableSelectors:
     - matchLabels:
         fpp-manager.cennso.com/plumbing-instance: upg-pbr-selector
   ```

For complete configuration details, see `UPGDeployment` CR examples for these configuration examples:

- [Policy-based routing](https://docs.cennso.com/api/assets/components/upg/3.2/assets/example-config/pb-routing-values.yaml)
- [FPPNetworks and RoutingTables](https://docs.cennso.com/api/assets/components/upg/3.2/assets/example-config/pb-additional-config.yaml).

## Verification

You can verify the policy-based routing configuration using the `vppctl` command in the UPG pod on the Core Cluster.

To verify that the SGi and VPN interfaces are configured correctly, run:

```bash
vppctl show interface address
```

Sample output:

```txt
grx (up):
  L3 198.18.0.16/24 ip4 table-id 100 fib-idx 2
inet (up):
  L3 172.20.0.16/24 ip4 table-id 200 fib-idx 1
  L3 fd71:357a:a82e:1::16/64 ip6 table-id 200 fib-idx 1
local0 (dn):
sxb (up):
  L3 172.18.0.16/24
vpn (up):
  L3 172.21.0.16/24 ip4 table-id 200 fib-idx 1
  L3 fd71:357a:a831:1::16/64 ip6 table-id 200 fib-idx 1
```

To verify that all routing tables exist, including the policy tables `201` and `202`, run:

```bash
vppctl show ip fib summary
```

Sample output:

```txt
ipv4-VRF:0, fib_index:0, flow hash:[src dst sport dport proto flowlabel ] epoch:0 flags:none locks:[adjacency:1, default-route:1, ]
    Prefix length         Count
                   0               1
                   4               2
                  24               1
                  32               6
ipv4-VRF:200, fib_index:1, flow hash:[src dst sport dport proto flowlabel ] epoch:0 flags:none locks:[interface:2, API:1, adjacency:1, ]
    Prefix length         Count
                   0               1
                   4               2
                  24               2
                  32               9
ipv4-VRF:100, fib_index:2, flow hash:[src dst sport dport proto flowlabel ] epoch:0 flags:none locks:[interface:1, API:1, ]
    Prefix length         Count
                   0               1
                   4               2
                  24               1
                  32               5
ipv4-VRF:201, fib_index:3, flow hash:[src dst sport dport proto flowlabel ] epoch:0 flags:none locks:[API:1, ]
    Prefix length         Count
                   0               1
                   4               2
                  24               1
                  32               3
ipv4-VRF:202, fib_index:4, flow hash:[src dst sport dport proto flowlabel ] epoch:0 flags:none locks:[API:1, ]
    Prefix length         Count
                   0               1
                   4               2
                  24               1
                  32               3
```

To verify that the forwarding policies are configured, run:

```bash
vppctl show upf policy
```

Sample output:

```txt
id:0 policy:defaultInet ip4_fib:3 ip6_fib:3 refs:0
id:1 policy:defaultVpn ip4_fib:4 ip6_fib:4 refs:0
id:2 policy:defaultInetIPFix ip4_fib:3 ip6_fib:3 refs:0
id:3 policy:defaultVpnIPFix ip4_fib:4 ip6_fib:4 refs:0
```
