> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/fpp/latest/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/latest/deployment-guides/bgp.md).

# Set up BGP

Border Gateway Protocol (BGP) is supported using the VPPTool Custom Resource which defines the tool (in the case of BGP, Bird application) to be deployed inside the FPP pod as a side container.

The FPP pod which supports BGP has an init container and multiple main containers. One is `fpp` main container, and the others are VPPTool containers like `tool-bird` or `tool-bird-exporter`.

FPP 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 the Linux kernel. The general model is that Linux is the network stack, which means that it has the control plane protocols, like ARP, IPv6 ND/MLD, ping, and so on, and VPP provides a SW-based ASIC for forwarding.

## BGP as VPPTool

To use BGP support in FPP, create `VPPTool` in the Core Cluster:

```yaml
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: VPPTool
metadata:
  name: bird
spec:
  image: registry.cennso.com/cennso/vpp-worker:v2.6.0
  command: ["/usr/local/bin/bird-tool.sh"]
  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: {}
```

## BGP metrics exporter

Bird Exporter is supported with another VPPTool:

```yaml
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: VPPTool
metadata:
  name: bird-exporter
spec:
  image: openvnf/bird_exporter:1.2.5
  command:
  - /bin/sh
  - -c
  - echo $$ >/${FPP_TOOL_PID_FILE} && [ -e "/run/bird/bird.ctl" ] && ip4=true || ip4=false; [ -e "/run/bird/bird6.ctl" ] && ip6=true || ip6=false; exec /app/bird_exporter -format.new=true -bird.socket /run/bird/bird.ctl -bird.socket6 /run/bird/bird6.ctl -bird.ipv4=$ip4 -bird.ipv6=$ip6
  resources:
    requests:
      cpu: "100m"
      memory: "128Mi"
    limits:
      cpu: "100m"
      memory: "128Mi"
  volumeMounts:
  - name: bird
    mountPath: /run/bird
```

>**CAUTION:** Modifying the VPPTool objects restarts all FPP pods that use it, which causes network downtime.

## Sample BGP IPv4 configuration

Reference the created BGP tools in CGWDeployment Custom Resource:

```yaml
apiVersion: fpp.cennso.com/v1alpha1
kind: CGWDeployment
metadata:
  name: cgw-bgp
spec:
  ...
  vppTools:
  - name: bird
  - name: bird-exporter

  bgpDaemon:
    metricsEnabled: true
```

The reference is based on VPPTool object name (`VPPTool.metadata.name`). BGP metrics needs to be enabled using **spec.bgpDaemon.metricsEnabled** set to `true`.

The following diagram shows an example of CGW with BGP deployment:

![CGW BGP Diagram](https://docs.cennso.com/api/assets/components/fpp/latest/assets/cgw-bgp-setup.svg)

See the full YAML configuration for the [CGW with BGP example](https://docs.cennso.com/api/assets/components/fpp/latest/assets/example-config/cgw-bgp-example.yaml):

```yaml
apiVersion: fpp.cennso.com/v1alpha1
kind: CGWDeployment
metadata:
  name: cgw-bgp
spec:

  version: v3.0.1
  targetNamespace: cgw-system
  repository: oci://registry.cennso.com/cennso/charts
  connectionSecretRef:
    name: test-core-cluster
    namespace: cennso-infra
  compositionRef:
    name: cgw-generic

  cnfName: cgw
  operatorName: node-op

  resources:
    cpu: "2"
    memory: 1Gi
    hugepages-2Mi: 1Gi

  heapSize: 500M
  useHugepages: false

  vppTools:
  - name: bird
  - name: bird-exporter

  bgpDaemon:
    metricsEnabled: true

  vpp:
    multicore: false
    dpdk:
      enabled: false
    traceAPICalls: true

  vppConfigs:
  - spec:
      toolConfigs:
      - name: bird
        configFiles:
        - name: bird.conf
          content: |
            router id 10.10.0.1;
            debug protocols all;
            protocol direct {
              interface "lo";
            }
            filter exclude_subnet {
              if net ~ [ 10.20.0.0/24 ] then reject;
              else accept;
            }
            protocol kernel {
              persist;
              scan time 20;
              import all;
              export filter exclude_subnet;
              kernel table 10;
            }
            protocol static {
              import all;
              export all;
              route 10.30.0.0/24 via 10.10.0.2;
              route 10.20.0.0/24 via 10.10.0.1;
            }
            protocol device {
              scan time 10;
            }
            protocol bgp neighbor_v4_1 {
              export all;
              import all;
              direct;
              gateway direct;
              local as 65001;
              neighbor 10.10.0.2 as 65001;
            }

  vppInterfaces:
  - ifName: mygre
    ips:
    - 10.10.0.1/24
    tableName: internal-rt
    greTunnel:
      sourceIf: external-nic
      destIP: 10.0.0.2
      encapTableName: external-rt
  - ifName: lcp0
    lcp:
      attachTo: mygre
      hostIfName: lcp0

  routingTables:
    external-rt:
      id: 50
      addressFamily: IPv4
    internal-rt:
      id: 10
      addressFamily: IPv4

  networks:
    external-nic:
      type: memif
      networkName: gre-outer-network
      tableName: external-rt
      ipv4:
        addresses:
        - 10.0.0.1/24
    internalnic:
      type: memif
      networkName: net-10
      tableName: internal-rt
      ipv4:
        addresses:
        - 10.20.0.1/24
```

## BGP configuration

The BGP configuration is kept in the VPPConfig object, which is created based on CGWDeployment's **spec.vppConfigs**, for example:

```yaml
apiVersion: fpp.cennso.com/v1alpha1
kind: CGWDeployment
metadata:
  name: cgw-bgp
spec:
  ...
  vppConfigs:
  - spec:
      toolConfigs:
      - name: bird
        configFiles:
        - name: bird.conf
          content: |
            router id 10.10.0.1;
            debug protocols all;
            protocol direct {
              interface "lo";
            }
            filter exclude_subnet {
              if net ~ [ 10.20.0.0/24 ] then reject;
              else accept;
            }
            protocol kernel {
              persist;
              scan time 20;
              import all;
              export filter exclude_subnet;
              kernel table 10;
            }
            protocol static {
              import all;
              export all;
              route 10.30.0.0/24 via 10.10.0.2;
              route 10.20.0.0/24 via 10.10.0.1;
            }
            protocol device {
              scan time 10;
            }
            protocol bgp neighbor_v4_1 {
              export all;
              import all;
              direct;
              gateway direct;
              local as 65001;
              neighbor 10.10.0.2 as 65001;
            }
```

**spec.vppConfigs[].spec.toolsConfigs[].name** must match VPPTool's **metadata.name**.

## BGP monitoring

To enable BGP metrics, set CGWDeployment **spec.bgpDaemon.metricsEnabled** to `true`.

CGW deployment pod exposes the metrics on port `9324`. It is using **bird-exporter** VPPTool.

BGP-related metrics are exposed in the `Routing` row of the [**CGW Overview** Grafana dashboard](https://docs.cennso.com/components/fpp/latest/observability/dashboard-cgw.md#routing).

## BGP over GRE

In complex scenarios, BGP peering can occur over a GRE tunnel, so a GRE interface must be created. Also, the LCP interface is required to be created for the BGP daemon to populate the routes into VPP:

```yaml
apiVersion: fpp.cennso.com/v1alpha1
kind: CGWDeployment
metadata:
  name: cgw-bgp
spec:
  ...
  vppInterfaces:
  - ifName: mygre
    ips:
    - 10.10.0.1/24
    tableName: internal-rt
    greTunnel:
      sourceIf: external-nic
      destIP: 10.0.0.2
      encapTableName: gre-outer-table
  - ifName: lcp0
    lcp:
      attachTo: mygre
      hostIfName: lcp0
```

In the above example, `10.10.0.1/24` is the IP address of the GRE tunnel endpoint using the `external-nic` interface to communicate to the other end of the GRE tunnel located at `10.0.0.2`. `external-nic` is a VPPInterface declared in CGWDeployment's **spec.networks** section.

It is important to use the same VPPInterface CR object name in **spec.vppInterfaces[].ifname** and in **spec.vppInterfaces[].lcp.attachTo**. This name will be adjusted with the prefix of the CGWDeployment name to be unique per cluster (`cgw-bgp-lcp0` or `cgw-bgp-mygre`, for example).

## Networking configuration

Declare **spec.routingTables** to create routing tables used for BGP deployment:

```yaml
apiVersion: fpp.cennso.com/v1alpha1
kind: CGWDeployment
metadata:
  name: cgw-bgp
spec:
  ...
  routingTables:
    external-rt:
      id: 50
      addressFamily: IPv4
    internal-rt:
      id: 10
      addressFamily: IPv4
```

The **spec.networks** creates Network Attachment Definitions (NADs), which are used together with FPP-CNI to provide VPPInterfaces for CGW:

```yaml
apiVersion: fpp.cennso.com/v1alpha1
kind: CGWDeployment
metadata:
  name: cgw-bgp
spec:
  ...
  networks:
    external-nic:
      type: memif
      networkName: gre-outer-network
      tableName: external-rt
      ipv4:
        addresses:
        - 10.0.0.1/24
    internal-nic:
      type: memif
      networkName: net-10
      tableName: internal-rt
      ipv4:
        addresses:
        - 10.20.0.1/24
```

See the configuration for FPPNetworks that need to be provided for CGW deployment (alongside the CGWDeployment object):

```yaml
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: FPPNetwork
metadata:
  name: net-10
spec:
  type: vxlan
  id: 10
---
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: FPPNetwork
metadata:
  name: gre-outer-network
spec:
  type: vxlan
  id: 42
```

## BGP IPv6 support

FPP 2.3.1 introduces the BGP IPv6 support. To enable this, you must create a new VPPTool with bird6:

```yaml
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: VPPTool
metadata:
  name: bird6
spec:
  image: registry.cennso.com/cennso/vpp-worker:v2.3.1
  env:
  - name: FPP_BIRD_BINARY
    value: bird6
  command: ["/usr/local/bin/bird-tool.sh"]
  configFiles:
  - name: bird6.conf
    canReload: true
  resources:
    requests:
      cpu: "100m"
      memory: "128Mi"
    limits:
      cpu: "100m"
      memory: "128Mi"
  volumeMounts:
  - name: bird
    mountPath: /run/bird
  volumes:
  - name: bird
    emptyDir: {}
```

## BGP dual stack

FPP supports dual IPv4 and IPv6 BGP configuration using two VPPTools: one for bird (IPv4) and a second for bird6 (IPv6). The BGP metrics exporter supports dual stack configuration.

>**CAUTION:** Make sure that **bird-exporter** VPPTool is updated as provided in [BGP metrics exporter section](#bgp-metrics-exporter).

The following diagram shows an example of CGW with BGP dual stack deployment:

![CGW BGP Dual Diagram](https://docs.cennso.com/api/assets/components/fpp/latest/assets/cgw-bgp-dual.svg)

The full BGP dual configuration is [included as an CGWDeployment object YAML](https://docs.cennso.com/api/assets/components/fpp/latest/assets/example-config/cgw-bgp-dual-example.yaml).

The difference between a single stack bird setup and a dual stack configuration is:

- Include `bird6` as VPPTool:

```yaml
apiVersion: fpp.cennso.com/v1alpha1
kind: CGWDeployment
metadata:
  name: cgw-bgp
spec:
  ...
  vppTools:
  - name: bird
  - name: bird6
  - name: bird-exporter
```

- Add BGP configuration:

```yaml
apiVersion: fpp.cennso.com/v1alpha1
kind: CGWDeployment
metadata:
  name: cgw-bgp
spec:
  ...
  vppConfigs:
  - spec:
      toolConfigs:
      - name: bird6
        configFiles:
        - name: bird6.conf
          content: |
            router id 10.10.0.1;
            debug protocols all;
            protocol device {
              scan time 10;
            }
            filter exclude_subnet {
              if net ~ [ 2001:db8:20::0/64 ] then reject;
              else accept;
            }
            protocol direct {
              check link yes;
              interface "lo";
            }
            protocol kernel {
              persist;
              scan time 20;
              import all;
              export filter exclude_subnet;
              kernel table 10;
            }
            protocol static {
              import all;
              export all;
              route 2001:db8:30::0/64 via 2001:db8:10::2;
              route 2001:db8:20::0/64 via 2001:db8:10::1;
            }
            protocol bgp neighbor_v6_1 {
              export all;
              import all;
              direct;
              gateway direct;
              local as 65001;
              neighbor 2001:db8:10::2 as 65001;
            }
```

- Add LCP1 interface connected to external-nic:

```yaml
apiVersion: fpp.cennso.com/v1alpha1
kind: CGWDeployment
metadata:
  name: cgw-bgp
spec:
  ...
  vppInterfaces:
  ...
  - ifName: lcp1
    lcp:
      attachTo: external-nic
      hostIfName: lcp1
```

- Configure the IPv6 addresses on internal and external NICs:

```yaml
apiVersion: fpp.cennso.com/v1alpha1
kind: CGWDeployment
metadata:
  name: cgw-bgp
spec:
  ...
  networks:
    external-nic:
      type: memif
      networkName: gre-outer-network
      tableName: external-rt
      ipv4:
        addresses:
        - 10.0.0.1/24
      ipv6:
        addresses:
        - 2001:db8:10::1/24
    internalnic:
      type: memif
      networkName: net-10
      tableName: internal-rt
      ipv4:
        addresses:
        - 10.20.0.1/24
      ipv6:
        addresses:
        - 2001:db8:20::1/24
```

## Bird2 support

FPP supports **bird2** using the **registry.cennso.com/cennso/nettools:v1.13.0** image. The new version of **bird2** supports a different BGP configuration than **bird** 1.6 used in the VPP Worker image. **bird2** enables the dual-stack configuration using a single binary (single VPPTool). A new VPPTool **bird2-exporter** is required to gather BGP metrics.

### Define bird2 VPPTool

Define the following VPPTool:

```yaml
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: VPPTool
metadata:
  name: bird2
spec:
  image: registry.cennso.com/cennso/nettools:v1.13.0
  command:
  - /bin/bash
  - -c
  - while [ ! -f "${FPP_TOOL_CONFIG_FILE_0}" ]; do sleep 1; done && exec nsenter --net="${FPP_LCP_NETNS}" bird -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: {}
```

### Define bird2-exporter

Bird exporter with bird2 support:

```yaml
apiVersion: fpp-manager.cennso.com/v1alpha1
kind: VPPTool
metadata:
  name: bird2-exporter
spec:
  image: openvnf/bird_exporter:1.2.5
  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
```

### Configure bird2

This diagram shows a sample CGW deployment used in the dual-stack configuration where bird2 supports the use of both IPv4 and IPv6 in a single binary (single VPPTool):

![CGW BGP Bird2 diagram](https://docs.cennso.com/api/assets/components/fpp/latest/assets/cgw-bgp-bird2.svg)

Sample BGP configuration for bird2 dual-stack IPv4 and IPv6:

```conf
router id 10.10.0.1;
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_subnet4 {
  if net ~ [ 10.20.0.0/24 ] then reject;
  else accept;
}
filter exclude_local_subnet4 {
  if net ~ [ 10.10.0.0/24 ] then reject;
  else accept;
}
filter exclude_subnet6 {
  if net ~ [ 2001:db8:20::0/64 ] then reject;
  else accept;
}
filter exclude_local_subnet6 {
  if net ~ [ 2001:db8:10::0/64 ] then reject;
  else accept;
}

protocol kernel kernel4 {
  ipv4 {
    # import all;
    import none;
    export filter exclude_subnet4;
  };
  persist;
  scan time 20;
  kernel table 10;
}
protocol kernel kernel6 {
  ipv6 {
    import all;
    export filter exclude_subnet6;
  };
  persist;
  scan time 20;
  kernel table 10;
}

protocol static static4 {
  ipv4 {
    import all;
    export filter exclude_local_subnet4;
  };
  route 10.10.0.2/32 via "lcp1";
  route 10.20.0.0/24 via 10.10.0.1;
}

protocol static static6 {
  ipv6 {
    import all;
    export filter exclude_local_subnet6;
  };
  route 2001:db8:10::2/128 via "lcp1";
  route 2001:db8:20::0/64 via 2001:db8:10::1;
}

protocol bgp neighbor_1 {
  local 10.10.0.1 as 65001;
  neighbor 10.10.0.2 as 65001;
  direct;
  ipv4 {
    export all;
    import all;
  };
  ipv6 {
    export all;
    import all;
    next hop address 2001:db8:10::2;
  };
  debug all;
}
```

The full BGP bird2 configuration is included as an [CGWDeployment object YAML](https://docs.cennso.com/api/assets/components/fpp/latest/assets/example-config/cgw-bgp-bird2-example.yaml).

The setup uses a single GRE tunnel for both IPv4 and IPv6. A single VPPTool is used, which uses a single `bird.conf` file.

## Verification

### Check the number of containers

The BGP-enabled VPP pod needs to have at least three containers: one main VPP Worker container (`fpp`), a container with a running BIRD process (`tool-bird`), and a `tool-bird-exporter` container that exposes metrics in Prometheus format. Also, the init container must be present. If IPv6 is also used, `tool-bird6` must be present as well.

### Check `tool-bird` container logs

Bird logs are visible in the `tool-bird` container logs of the FPP pod.

### Check the applied BGP configuration

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

```bash
vppctl show ip fib table 10
```

Table number (`10`) must match the `internal-rt` table created above.

See the output example:

```
...
10.30.0.0/24
  unicast-ip4-chain
  [@0]: dpo-load-balance: [proto:ip4 index:39 buckets:1 uRPF:51 to:[10:840]]
    [0] [@6]: ipv4 via 0.0.0.0 mygre: mtu:9000 next:3 flags:[] 4500000000000000fe2fa8cc0a0000010a00000200000800
        stacked-on entry:25:
          [@4]: ipv4 via 10.0.0.2 external-nic: mtu:9000 next:5 flags:[] 026703d9663a026703efbec20800
...
```

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

```bash
birdc show route
```

```bash
birdc show protocol
```

```bash
birdc show protocols all neighbor_v4_1
```

See the output example:

```bash
# birdc show route
BIRD 1.6.8 ready.
10.20.0.0/24       via 10.10.0.1 on lcp0 [static1 15:10:53] * (200)
10.30.0.0/24       via 10.10.0.2 on lcp0 [static1 15:10:53] * (200)

# birdc show protocol
BIRD 1.6.8 ready.
name     proto    table    state  since       info
device1  Device   master   up     15:10:49
direct1  Direct   master   up     15:10:53
static1  Static   master   up     15:10:53
neighbor_v4_1 BGP      master   up     15:16:18    Established
kernel1  Kernel   master   up     15:10:53

# birdc show protocols all neighbor_v4_1
neighbor_v4_1 BGP      master   up     15:16:18    Established
  Preference:     100
  Input filter:   ACCEPT
  Output filter:  ACCEPT
  Routes:         0 imported, 2 exported, 0 preferred
  Route change stats:     received   rejected   filtered    ignored   accepted
    Import updates:              0          0          0          0          0
    Import withdraws:            0          0        ---          0          0
    Export updates:              2          0          0        ---          2
    Export withdraws:            0        ---        ---        ---          0
  BGP state:          Established
    Neighbor address: 10.10.0.2
    Neighbor AS:      65001
    Neighbor ID:      10.10.0.2
    Neighbor caps:    refresh enhanced-refresh restart-aware llgr-aware AS4
    Session:          internal AS4
    Source address:   10.10.0.1
    Hold timer:       199/240
    Keepalive timer:  44/80

```
