> 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/troubleshooting/traffic-capturing.md).

# Traffic Capturing

To capture traffic in an easy way, you can use the NetCap extension to collect and aggregate traffic from multiple worker nodes in a central location. Follow the [NetCap documentation page](https://docs.cennso.com/cennso/netcap/main.md) and [FPP configuration for NetCap](https://docs.cennso.com/cennso/fpp/main/operation-guides/netcap-traffic-capturing.md) for more information.

Traffic on UPG is also handled inside the VPP framework. UPG pods run in the Core Cluster, and the commands below should be executed there. Tools to capture traffic using the kernel network stack (like `tcpdump` or `ip`) are not useful for UPG debugging.

## VPP Packet Tracing

There are many commands in VPP CLI for debugging, and most of them are documented in [FPP Troubleshooting docs](https://docs.cennso.com/cennso/fpp/main/troubleshooting/traffic-capture.md).

Commands available there:

- `trace add <input> <num>`
- `show trace`
- `clear trace`
- `pcap trace`

> **Warning**
>
> `trace` and `pcap trace` CLI commands use memory to store the trace, so before using it, assess how much
> memory is left on the pod. The alternative is to use kernel based pcap capturing inside workload pod that
> uses the TAP interface.

[VPP trace facility](https://s3-docs.fd.io/vpp/22.06/gettingstarted/progressivevpp/traces.html)
can be used for in-depth debugging of network connectivity.

Dispatch trace VPP facility captures the traversal of the VPP node graph by the buffers (packets), e.g.:

```bash
vppctl pcap dispatch trace on max 10000 file trace-file.pcap buffer-trace memif-input 100
```

The command above can take up to 20-30Mb RAM to gather the traffic. To stop
the trace, run:

```bash
vppctl pcap dispatch trace off
```

The result is saved in `/tmp/trace-file.pcap`.

For later analysis, this file needs to be copied from the pod to an external machine by hand, for example, by the `kubectl cp` command:

```bash
kubectl cp -n {UPG_NS} {UPG_POD_NAME}:/tmp/trace-file.pcap ./trace-file.pcap
```

## Using SPAN interfaces

UPG traffic can be captured using SPAN interfaces that mirror all traffic from the UPG memif to the kernel-based TAP interface, making it possible to capture traffic using a standard Linux pod with tshark/tcpdump tools.

To understand better how to do it, let's define an example:

- UPGDeployment installed using name `upg-tdf`, instance name (**spec.instances**) set to `inst01`
- UPG in TDF mode, with pod name `{UPG-POD-NAME}`, with above setting it equals to `upg-tdf-inst01-0`
- Access network VPPInterface CR named `upg-tdf-inst01-access`
- Control network VPPInterface CR named `upg-tdf-inst01-sxb`
- Data network interface named `upg-tdf-inst01-pool-at-sgi-at`
- Second data interface called `upg-tdf-inst01-pool-de-sgi-de`
- UPG runs in namespace `{UPG-NS}`
- UPG is deployed on `{NODE}`

First, define a proper Network Attachment Definition in the Core Cluster, for example:

```yaml
apiVersion: k8s.cni.cncf.io/v1
kind: NetworkAttachmentDefinition
metadata:
  name: upg-sxb
  namespace: {UPG-NS}
spec:
  config: |-
    {
      "cniVersion": "0.3.0",
      "ipam": {
        "addresses": [
          {
            "address": "10.0.0.3/24"
          }
        ],
        "type": "static"
      },
      "mtu": 9000,
      "name": "span-upg-sxb",
      "type": "fpp-cni",
      "vni": 1,
      "vppIfaceType": "tap",
      "spanSource": {
        "vppInterfaceRef": {
          "name": "upg-tdf-inst01-sxb",
          "namespace": "{UPG-NS}"
        }
      }
    }
---
apiVersion: k8s.cni.cncf.io/v1
kind: NetworkAttachmentDefinition
metadata:
  name: upg-data-at
  namespace: {UPG-NS}
spec:
  config: |-
    {
      "cniVersion": "0.3.0",
      "ipam": {
        "addresses": [
          {
            "address": "10.0.0.4/24"
          }
        ],
        "type": "static"
      },
      "mtu": 9000,
      "name": "span-upg-data-at1",
      "type": "fpp-cni",
      "vni": 1,
      "vppIfaceType": "tap",
      "spanSource": {
        "vppInterfaceRef": {
          "name": "upg-tdf-inst01-pool-at-sgi-at",
          "namespace": "{UPG-NS}"
        }
      }
    }
---
apiVersion: k8s.cni.cncf.io/v1
kind: NetworkAttachmentDefinition
metadata:
  name: upg-data-de
  namespace: {UPG-NS}
spec:
  config: |-
    {
      "cniVersion": "0.3.0",
      "ipam": {
        "addresses": [
          {
            "address": "10.0.0.5/24"
          }
        ],
        "type": "static"
      },
      "mtu": 9000,
      "name": "span-upg-data-de1",
      "type": "fpp-cni",
      "vni": 1,
      "vppIfaceType": "tap",
      "spanSource": {
        "vppInterfaceRef": {
          "name": "upg-tdf-inst01-pool-de-sgi-de",
          "namespace": "{UPG-NS}"
        }
      }
    }
---
apiVersion: k8s.cni.cncf.io/v1
kind: NetworkAttachmentDefinition
metadata:
  name: upg-access
  namespace: {UPG-NS}
spec:
  config: |-
    {
      "cniVersion": "0.3.0",
      "ipam": {
        "addresses": [
          {
            "address": "10.0.0.6/24"
          }
        ],
        "type": "static"
      },
      "mtu": 9000,
      "name": "span-upg-access",
      "type": "fpp-cni",
      "vni": 1,
      "vppIfaceType": "tap",
      "spanSource": {
        "vppInterfaceRef": {
          "name": "upg-tdf-inst01-access",
          "namespace": "{UPG-NS}"
        }
      }
    }

```

This NADS must be used with a Linux pod and scheduled on the same node that UPG is working on (`{NODE}`):

```yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: upg-span
  namespace: {UPG-NS}
spec:
  podManagementPolicy: Parallel
  replicas: 1
  selector:
    matchLabels:
      name: upg-span
  updateStrategy:
    type: RollingUpdate
  serviceName: upg-span
  template:
    metadata:
      annotations:
        k8s.v1.cni.cncf.io/networks: upg-sxb@sxb,upg-access@an,upg-data-de1@de1,upg-data-at1@at1
      labels:
        name: upg-span
    spec:
      containers:
      - name: span
        command: ["/bin/sh", "-x", "-c", "ip a && while true; do sleep 1000; done"]
        image: quay.io/travelping/nettools:v1.10.0
        imagePullPolicy: IfNotPresent
        securityContext:
          privileged: true
      nodeSelector:
        kubernetes.io/hostname: {NODE}
```

Inside the SPAN pod, traffic from the access network is mirrored on the `an` interface, the data network is mirrored
to interfaces `de1` and `at1`, and the control network traffic is available on the `sxb` interface.
