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

# BGP support

User Plane Gateway (UPG) uses the [BIRD2](https://github.com/CZ-NIC/bird) BGP daemon in a sidecar container to support Border Gateway Protocol (BGP). User Equipment IPs (UE-IPs) are exported and can be announced through BGP to edge gateways.

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

- Main container `upg-vpp`
- BGP daemon container `tool-bird2`
- BGP metrics exporter container `tool-bird-exporter`

UPG uses [the 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 learnt from BGP peering to the VPP routing table. The other direction is also possible: UE-IPs are exported and can be announced through BGP to edge gateways.

## Sample BGP configuration

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

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

In the basic 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 CGW on the right.

The full UPG configuration with enabled BGP is available [here](https://docs.cennso.com/api/assets/components/upg/2.16/assets/example-config/pgw-bgp.yaml). Follow the instructions in the sections below to configure BGP.

### Configure networking

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

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

See the example of networking configuration:

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

### Enable BGP

Use this section of the UPG Helm chart to enable BGP deployment:

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

Setting **bgp.enabled** to `true` enables BGP support. This adds the `bird2` and `bird-exporter` sidecar containers to the UPG deployment and connects them using the `/run/bird` shared volume. In **bgp.netnsPath**, you can specify a common Linux network namespace. The network namespace and **bgp.table** are passed to UPG-VPP to configure UE IPs publishing. The **bgp.interface** is used to create the LCP interface connected to the VPPInterface. In this example, `sgi` corresponds to **instances.{name}.networking.sgi.interface**.

You can set images for the containers related to the BPG functionality:

```yaml
bgp:
  # Specifies the container image that includes the `bird2` executable. For the UPG deployment, this is associated with the `tool-bird2` container.
  image:
    repository: registry.cennso.com/cennso/nettools
    tag: v1.13.0
    pullPolicy: IfNotPresent
  # Specifies the container image with the LCP namespace setup script. For the UPG deployment, this is associated with the `setup` container.
  setup:
    image:
      repository: registry.cennso.com/cennso/fpp-manager
      tag: v2.6.0
      pullPolicy: IfNotPresent
  # Specifies the container image with the BIRD metrics exporter. For the UPG deployment, this is associated with the `tool-bird-exporter` container.
  exporter:
    image:
      repository: registry.cennso.com/cennso/bird_exporter
      tag: 1.2.6
      pullPolicy: IfNotPresent
```

### Define the UPGConfig representation

The Helm chart values defined in the **bgp** section are mapped to the UPGConfig Custom Resource in **spec.ueip.export**:

```yaml
apiVersion: upg-manager.cennso.com/v1alpha1
kind: UPGConfig
metadata:
  name: upg-release-name-prox01
spec:
...
  ueip:
    export:
      ns-routes:
        enable: true # by default false
        netnsPath: /run/vpptools/netns
        table: 213
...
```

### Use debug CLI

>**NOTE:** Configuration applied through UPGConfig overrides the CLI configuration.

You can configure the UE IPs export at runtime using VPPCTL commands. See the command example to enable the UE IPs route exposure:

```bash
vppctl upf ueip export as-ns-route enable netns-path ${netnsPath} table ${id}
```

Use this command to deactivate the UE IP export:

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

### Configure BGP

Provide the BGP daemon configuration in **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
instances:
  prox01:
    bgp:
      config: {BIRD2 CONFIGURATION}
```

Sample BIRD2 configuration:

```
# 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;
}
```

## Setup verification

### Check the number of containers

The UPG pod with BGP support includes three containers:

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

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

### Check UE IP exposure

To check if UPG is configured to publish UE IPs, use a VPPCTL command `show upf ueip export as-ns-route`.

See the output example:

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

You can check the UPG's `tool-bird2` container logs where the exported UE IPs are listed. See the sample log message:

```
bird: kernel1: Pruning table master
bird: kernel1: Pruning inherited routes
bird: device1: Scanning interfaces
bird: kernel1: Scanning routing table
bird: kernel1: 10.0.117.8/32: [alien] seen
bird: kernel1: 10.1.212.33/32: [alien] seen
bird: kernel1: 10.3.228.219/32: [alien] seen
bird: kernel1: 10.10.48.36/32: [alien] seen
bird: kernel1: 10.12.101.246/32: [alien] seen
bird: ...
```

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

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

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

Table number (`213`) must match the **bgp.table** configured above.

Sample output:

```bash
# ip route show table 213
10.2.62.89 dev lcp0 proto static scope link
10.3.125.49 dev lcp0 proto static scope link
10.6.252.74 dev lcp0 proto static scope link
10.9.18.37 dev lcp0 proto static scope link
10.12.145.13 dev lcp0 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 lcp0 proto static metric 1024 pref medium
2a07:db01:44:15bb::/64 dev lcp0 proto static metric 1024 pref medium
2a07:db01:44:1c68::/64 dev lcp0 proto static metric 1024 pref medium
2a07:db01:44:1e70::/64 dev lcp0 proto static metric 1024 pref medium
2a07:db01:44:5533::/64 dev lcp0 proto static metric 1024 pref medium
```

### Check the applied BGP configuration

To check if BGP configuration and peering is active, run this command in the `upg-vpp` container of the UPG pod:

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

Table number (`213`) must match the **bgp.table** configured above.

See the output example:

```
...
10.0.117.8/32
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:78 buckets:1 uRPF:116 to:[0:0]]
    [0] [@3]: arp-ipv4: via 10.0.117.8 sgi
10.1.212.33/32
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:57 buckets:1 uRPF:121 to:[0:0]]
    [0] [@3]: arp-ipv4: via 10.1.212.33 sgi
10.3.228.219/32
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:65 buckets:1 uRPF:103 to:[0:0]]
    [0] [@3]: arp-ipv4: via 10.3.228.219 sgi
...
```

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 lcp0
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 lcp0
10.1.230.132/32      unicast [kernel4 09:23:34.649] * (10)
        dev lcp0
10.28.228.76/32      unicast [kernel4 09:23:35.653] * (10)
        dev lcp0
10.17.122.202/32     unicast [kernel4 09:23:33.654] * (10)
        dev lcp0
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 lcp0
2a07:db01:44:e15e::/64 unicast [kernel6 09:23:34.650] * (10)
        dev lcp0
2a07:db01:44:551d::/64 unicast [kernel6 09:23:35.654] * (10)
        dev lcp0
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 lcp0
2a07:db01:44:6e02::/64 unicast [kernel6 09:23:31.654] * (10)
        dev lcp0
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
```
