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

# Enable BGP support

User Plane Gateway (UPG) supports Border Gateway Protocol (BGP) with the [BIRD2](https://github.com/CZ-NIC/bird) daemon, which runs in a `VPPTool` as a sidecar container. UPG exports the User Equipment (UE) IP addresses, and the BGP daemon can announce them to edge gateways.

The UPG pod with BGP support includes an init container and several main containers:

- Main container `fpp`
- BGP daemon container using VPPTool `bird2`
- BGP metrics exporter container using VPPTool `bird2-exporter`

UPG uses [the Vector Packet Processing (VPP) Linux Control Plane (LCP) plugin](https://s3-docs.fd.io/vpp/24.02/developer/plugins/lcp.html). This plugin allows VPP to integrate with applications that use the Linux kernel network stack. In the BGP support use case, the BGP daemon inserts routes learned from BGP peering into the VPP routing table. In the other direction, the BGP daemon announces the [exported](https://docs.cennso.com/components/upg/3.2/concepts/upgconfig.md#ue-ip-export) UE IP addresses to edge gateways.

## Configuration

The following diagram shows the UPG deployment with the BGP support:

![BGP support integration diagram](https://docs.cennso.com/api/assets/components/upg/3.2/assets/upg-bgp-setup.svg)

In the basic Packet Gateway (PGW) example, the BGP daemon is connected to the SGI interface. The LCP interface is directly attached to SGI, and the BGP daemon peers with the CGW shown in the diagram.

For the full UPG configuration with BGP enabled, see the [PGW configuration example with BGP](https://docs.cennso.com/api/assets/components/upg/3.2/assets/example-config/pgw-bgp.yaml). To configure BGP, follow the instructions in the next sections.

### Configure networking

First, configure networking. Make sure to provide the following values:

- **spec.instances.{name}.networking.sgi.interface** - name of the interface to connect the BGP daemon to
- **spec.instances.{name}.networking.sgi.ipv4.addresses** - SGI interface IPs used in the BGP configuration

See the example of networking configuration:

```yaml
spec:
  instances:
    prox01:
      networking:
        grx:
          networkName: grx40
          mtu: 8930
          interface: grx
          ipv4:
            addresses:
              - 198.18.0.20/24
        sgi:
          networkName: sgi41
          mtu: 8930
          interface: sgi
          ipv4:
            addresses:
            - 198.19.0.20/24
        sx:
          interface: sxb
          networkName: sx39
          ipv4:
            addresses:
              - 172.20.0.20/24
```

### Enable BGP

Use this section of the UPGDeployment CR, applied to the Management Cluster, to enable BGP deployment:

```yaml
spec:
  bgp:
    # -- Enables UE IP publishing by BGP daemon
    enabled: true
    # -- Linux network namespace shared between UPG and the BGP daemon
    netnsPath: /run/vpptools/netns
    # -- Linux kernel table ID where UPG publishes UE IPs
    table: 213
    # -- Interface to which the BGP daemon is attached
    interface: sgi
    # -- Linux interface that the exported UE IP routes point to
    exportInterface: lo
```

If you set **spec.bgp.enabled** to `true`, the UPG pod gets the `bird2` and `bird2-exporter` `VPPTool` resources as sidecar containers, connected through the `/run/bird` shared volume. In **spec.bgp.netnsPath**, you can specify a common Linux network namespace. UPG uses this network namespace and **spec.bgp.table** to export the UE IP addresses. UPG creates the LCP pair for the interface that **spec.bgp.interface** names. In this example, `sgi` corresponds to **spec.instances.{name}.networking.sgi.interface**.

The **spec.bgp.exportInterface** parameter sets the Linux interface in **spec.bgp.netnsPath** that the exported UE IP routes point to. The default is `lo`. The BGP daemon reads the routes from the kernel table. It announces them with its own next hop, independent of the interface.

The interface must exist in the network namespace and be up. If it does not, the kernel rejects the IPv4 routes. Do not use an LCP interface such as `lcp0`. The LCP plugin would import every UE IP route into VPP, and VPP would then send an Address Resolution Protocol (ARP) request on `sgi` for each UE IP address.

> [!IMPORTANT]
> If VPP restarts inside a running UPG pod, the exported UE IP routes stay on the export interface in **spec.bgp.table**.
> UPG does not remove them afterward, so the BGP daemon keeps announcing UE IP addresses whose sessions no longer exist on this UPG.
> To remove these routes, recreate the UPG pod, which drops all sessions of the UPG instance.
> For details, see the [known issue in the UPG 3.2.0 release notes](https://docs.cennso.com/components/upg/3.2/releases/3.2.0.md#exported-ue-ip-routes-stay-after-a-vpp-restart).

You can set VPPTools used for BGP:

```yaml
spec:
  bgp:
    bgpTools:
    # First is a BGP daemon VPPTool
    - bird2
    # Second is a BGP metrics exporter VPPTool
    - bird2-exporter
```

You can use the following VPPTool examples, if they are not already deployed in the Core Cluster:

```yaml
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: VPPTool
metadata:
  name: bird2
spec:
  image: registry.cennso.com/cennso/nettools:v1.13.0
  env:
  - name: FPP_BIRD_BINARY
    value: bird
  command:
  - /bin/bash
  - -c
  - while [ ! -f "${FPP_TOOL_CONFIG_FILE_0}" ]; do sleep 1; echo "waiting for config file ${FPP_TOOL_CONFIG_FILE_0}"; done && echo "run bird"; exec nsenter --net="${FPP_LCP_NETNS}" ${FPP_BIRD_BINARY} -P "${FPP_TOOL_PID_FILE}" -c "${FPP_TOOL_CONFIG_FILE_0}" -s /run/bird/bird.ctl -f -d
  configFiles:
  - name: bird.conf
    canReload: true
  resources:
    requests:
      cpu: "100m"
      memory: "128Mi"
    limits:
      cpu: "100m"
      memory: "128Mi"
  volumeMounts:
  - name: bird
    mountPath: /run/bird
  volumes:
  - name: bird
    emptyDir: {}
---
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: VPPTool
metadata:
  name: bird2-exporter
spec:
  image: registry.cennso.com/cennso/bird_exporter:v1.2.6
  command:
  - /bin/sh
  - -c
  - "echo $$ >/${FPP_TOOL_PID_FILE} && exec /app/bird_exporter -format.new=true -bird.socket /run/bird/bird.ctl -bird.v2"
  resources:
    requests:
      cpu: "100m"
      memory: "128Mi"
    limits:
      cpu: "100m"
      memory: "128Mi"
  volumeMounts:
  - name: bird
    mountPath: /run/bird
```

### Define the UPGConfig representation

The `UPGDeployment` CR values defined in the **spec.bgp** section are mapped to the `UPGConfig` Custom Resource in **spec.ueip.nsExport**:

```yaml
apiVersion: upg-manager.cennso.com/v1alpha1
kind: UPGConfig
metadata:
  name: upg-release-name-prox01
spec:
...
  ueip:
    nsExport:
      enabled: true # by default false
      netnsPath: /run/vpptools/netns
      tableID: 213
      ifName: lo
...
```

### Use debug CLI

> [!NOTE]
> Configuration applied through `UPGConfig` overrides the CLI configuration.

You can configure the export of UE IP addresses at runtime using `vppctl` commands. To enable the export of UE IP routes, run:

```bash
vppctl upf ueip export as-ns-route enable ns-path {NETNS_PATH} table {TABLE_ID} if-name {INTERFACE_NAME}
```

Replace the placeholders:

- `{NETNS_PATH}`: the Linux network namespace, for example `/run/vpptools/netns`
- `{TABLE_ID}`: the kernel table, for example `213`
- `{INTERFACE_NAME}`: the Linux interface that the exported routes point to, for example `lo`

Use this command to deactivate the export of UE IP addresses:

```bash
vppctl upf ueip export as-ns-route disable
```

### Configure BGP

Provide the BGP daemon configuration in **spec.instances.{name}.bgp.config** as a BIRD2 configuration file. The configuration is exposed as a ConfigMap and attached to the UPG deployment. The UPG image includes a script that checks for configuration updates and restarts the BGP daemon if any changes are detected.

Add the following BGP configuration to the UPG instance:

```yaml
spec:
  instances:
    prox01:
      bgp:
        config: |
          {BIRD2 CONFIGURATION}
```

Sample BIRD2 configuration:

```text
# ID is a string which represents UPG SGI IP
router id 198.19.0.20;

# Comment to disable debug logging
debug protocols all;
log syslog { debug, trace, info, remote, warning, error, auth, fatal, bug };
log stderr all;

protocol device {
  scan time 10;
}
protocol direct {
  interface "lo";
  ipv4;
  ipv6;
}

filter exclude_local_subnet4 {
  if net ~ [ 198.19.0.0/24 ] then reject;
  else accept;
}
filter exclude_local_subnet6 {
  if net ~ [ 2001:db8:19::0/64 ] then reject;
  else accept;
}

protocol kernel kernel4 {
  ipv4 {
    # read from kernel routes
    import all;
    # add routes from BGP to kernel
    # filter directly connected to SGI interface subnet
    export filter exclude_local_subnet4;
  };
  persist;
  # Add routes existing in kernel to BGP daemon announced routes
  learn;
  scan time 20;
  # Linux kernel table ID where UPG publishes UE IPs
  kernel table 213;
}
protocol kernel kernel6 {
  ipv6 {
    import all;
    export filter exclude_local_subnet6;
  };
  persist;
  learn;
  scan time 20;
  kernel table 213;
}

protocol bgp neighbor_1 {
  # local is SGI interface IP
  local 198.19.0.20 as 65001;
  # neighbor is BGP peer IP (CGW-SGI in example)
  neighbor 198.19.0.1 as 65001;
  ipv4 {
    export all;
    import all;
  };
  ipv6 {
    export all;
    import all;
    # IPv6 of SGI interface
    next hop address 2001:db8:19::20;
  };
  debug all;
}
```

## Verification

### Check the number of containers

The UPG pod with BGP support includes three containers:

- Main FPP container (`fpp`)
- Container with a running BIRD process (`tool-bird2`)
- Container that exposes metrics in the Prometheus format (`tool-bird2-exporter`)

Check the number of containers to verify if BGP is up and running.

### Check UE IP exposure

To check whether UPG publishes UE IP addresses, run the following `vppctl` command:

```bash
vppctl show upf ueip export as-ns-route
```

See the output example:

```text
UEIP export as-ns-route status: enabled
  Network namespace: /run/vpptools/netns
  Interface: lo (id 1)
  Table: 213
  Routes added: 40
```

The `tool-bird2` container logs of UPG list the exported UE IP addresses. See the sample log message:

```text
bird: kernel4: 10.11.113.46/32: [alien async] created
bird: kernel4.ipv4 > added [best] 10.11.113.46/32 0L 3G unicast
bird: neighbor_1.ipv4 < added 10.11.113.46/32 0L 3G unicast
bird: ...
bird: kernel4: 10.14.109.51/32: [alien async] removed
bird: kernel4.ipv4 > removed [sole] 10.14.109.51/32 0L 3G unicast
bird: neighbor_1.ipv4 < removed 10.14.109.51/32 0L 3G unicast
bird: ...
```

To check the list of exported UE IP addresses, use the Linux command `ip route` in the `tool-bird2` container:

```bash
ip route show table 213
```

```bash
ip -6 route show table 213
```

The table number (`213`) must match **spec.bgp.table** from [Enable BGP](#enable-bgp).

Sample output:

```bash
# ip route show table 213
10.2.62.89 dev lo proto static scope link
10.3.125.49 dev lo proto static scope link
10.6.252.74 dev lo proto static scope link
10.9.18.37 dev lo proto static scope link
10.12.145.13 dev lo proto static scope link
10.40.0.0/24 via 198.19.0.1 dev lcp0 proto bird metric 32
198.19.0.20 via 198.19.0.1 dev lcp0 proto bird metric 32
# ip -6 route show table 213
2001:db8:19::20 via 2001:db8:19::1 dev lcp0 proto bird metric 32 pref medium
2001:db8:40::/64 via 2001:db8:19::1 dev lcp0 proto bird metric 32 pref medium
2a07:db01:44:4cd::/64 dev lo proto static metric 1024 pref medium
2a07:db01:44:15bb::/64 dev lo proto static metric 1024 pref medium
2a07:db01:44:1c68::/64 dev lo proto static metric 1024 pref medium
2a07:db01:44:1e70::/64 dev lo proto static metric 1024 pref medium
2a07:db01:44:5533::/64 dev lo proto static metric 1024 pref medium
```

> [!NOTE]
> If UPG has no sessions yet, the routing table might not exist, because UPG adds a UE IP route for each session.

### Check the applied BGP configuration

To check if the BGP configuration and peering are active, run this command in the `fpp` container of the UPG pod:

```bash
vppctl show ip fib table 213
```

The table number (`213`) must match **spec.bgp.table** from [Enable BGP](#enable-bgp).

The table shows the routes that UPG learned over BGP, for example `10.40.0.0/24`.
UE IP addresses do not appear in this table, because UPG exports them on `lo`, which has no LCP pair.

See the output example:

```text
ipv4-VRF:213, fib_index:3, flow hash:[src dst sport dport proto flowlabel ] epoch:0 flags:none locks:[lcp-rt:1, ]
0.0.0.0/0
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:40 buckets:1 uRPF:46 to:[0:0]]
    [0] [@0]: dpo-drop ip4
0.0.0.0/32
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:41 buckets:1 uRPF:60 to:[0:0]]
    [0] [@0]: dpo-drop ip4
10.40.0.0/24
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:45 buckets:1 uRPF:59 to:[0:0]]
    [0] [@5]: ipv4 via 198.19.0.1 sgi: mtu:8930 next:4 flags:[] 02fef9f3d5ba02fe13eb4dcf0800
224.0.0.0/4
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:43 buckets:1 uRPF:62 to:[0:0]]
    [0] [@0]: dpo-drop ip4
240.0.0.0/4
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:42 buckets:1 uRPF:61 to:[0:0]]
    [0] [@0]: dpo-drop ip4
255.255.255.255/32
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:44 buckets:1 uRPF:64 to:[0:0]]
    [0] [@12]: dpo-receive: 0.0.0.0 on local0
```

To check BGP peering on the `tool-bird2` container, use the `birdc` diagnostic tool to get the state:

- See the BGP routes exchanges between peers:

```bash
birdc -s /run/bird/bird.ctl show route
```

- Show the local protocols and BGP peering state

```bash
birdc -s /run/bird/bird.ctl show protocol
```

- See the detailed information about BGP:

```bash
birdc -s /run/bird/bird.ctl show protocols all neighbor_1
```

See the output example:

```bash
# birdc -s /run/bird/bird.ctl show route
BIRD 2.15.1 ready.
Table master4:
10.7.186.249/32      unicast [kernel4 09:23:31.653] * (10)
        dev lo
198.19.0.20/32       unicast [neighbor_1 09:18:10.348] * (100) [i]
        via 198.19.0.1 on lcp0
10.14.255.52/32      unicast [kernel4 09:23:32.653] * (10)
        dev lo
10.1.230.132/32      unicast [kernel4 09:23:34.649] * (10)
        dev lo
10.28.228.76/32      unicast [kernel4 09:23:35.653] * (10)
        dev lo
10.17.122.202/32     unicast [kernel4 09:23:33.654] * (10)
        dev lo
10.40.0.0/24         unicast [neighbor_1 09:18:10.348] * (100) [i]
        via 198.19.0.1 on lcp0

Table master6:
2a07:db01:44:19df::/64 unicast [kernel6 09:23:33.654] * (10)
        dev lo
2a07:db01:44:e15e::/64 unicast [kernel6 09:23:34.650] * (10)
        dev lo
2a07:db01:44:551d::/64 unicast [kernel6 09:23:35.654] * (10)
        dev lo
2001:db8:40::/64     unicast [neighbor_1 09:18:10.349 from 198.19.0.1] * (100) [i]
        via 2001:db8:19::1 on lcp0
2a07:db01:44:522b::/64 unicast [kernel6 09:23:32.654] * (10)
        dev lo
2a07:db01:44:6e02::/64 unicast [kernel6 09:23:31.654] * (10)
        dev lo
2001:db8:19::20/128  unicast [neighbor_1 09:18:10.391 from 198.19.0.1] * (100) [i]
        via 2001:db8:19::1 on lcp0

# birdc -s /run/bird/bird.ctl show protocol
BIRD 2.15.1 ready.
Name       Proto      Table      State  Since         Info
device1    Device     ---        up     12:14:05.800
direct1    Direct     ---        up     12:14:05.800
kernel4    Kernel     master4    up     12:14:05.800
kernel6    Kernel     master6    up     12:14:05.800
neighbor_1 BGP        ---        up     12:15:57.550  Established

# birdc -s /run/bird/bird.ctl show protocols all
BIRD 2.15.1 ready.
Name       Proto      Table      State  Since         Info
neighbor_1 BGP        ---        up     09:18:10.348  Established
  BGP state:          Established
    Neighbor address: 198.19.0.1
    Neighbor AS:      65001
    Local AS:         65001
    Neighbor ID:      198.19.0.1
    Local capabilities
      Multiprotocol
        AF announced: ipv4 ipv6
      Route refresh
      Graceful restart
      4-octet AS numbers
      Enhanced refresh
      Long-lived graceful restart
    Neighbor capabilities
      Multiprotocol
        AF announced: ipv4 ipv6
      Route refresh
      Graceful restart
      4-octet AS numbers
      Enhanced refresh
      Long-lived graceful restart
    Session:          internal AS4
    Source address:   198.19.0.20
    Hold timer:       187.286/240
    Keepalive timer:  43.781/80
    Send hold timer:  429.868/480
  Channel ipv4
    State:          UP
    Table:          master4
    Preference:     100
    Input filter:   ACCEPT
    Output filter:  ACCEPT
    Routes:         2 imported, 5 exported, 2 preferred
    Route change stats:     received   rejected   filtered    ignored   accepted
      Import updates:              2          0          0          0          2
      Import withdraws:            0          0        ---          0          0
      Export updates:             17          2          0        ---         15
      Export withdraws:           10        ---        ---        ---         10
    BGP Next hop:   198.19.0.20
  Channel ipv6
    State:          UP
    Table:          master6
    Preference:     100
    Input filter:   ACCEPT
    Output filter:  ACCEPT
    Routes:         2 imported, 5 exported, 2 preferred
    Route change stats:     received   rejected   filtered    ignored   accepted
      Import updates:              2          0          0          0          2
      Import withdraws:            0          0        ---          0          0
      Export updates:             17          2          0        ---         15
      Export withdraws:           10        ---        ---        ---         10
    BGP Next hop:   2001:db8:19::20 fe80::8ff2:7250:f02:7ca0
```
