> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/upg/2.17/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.17/troubleshooting/traffic-capturing.md).

# Traffic Capturing

Traffic on UPG is handled only inside the VPP framework. Tools to capture traffic using 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.

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 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 `pcap dispatch` trace off`, and 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 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:

- UPG in TDF mode, with pod name `{UPG-POD-NAME}`
- Access network interface named `an`
- Control network interface named `sxb`
- Data network interface named `at1`
- Second data interface called `de1`
- UPG runs in namespace `{UPG-NS}`
- UPG is deployed on `{NODE}`

Firstly, define a proper Network Attachment Definition, 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": {
        "matchingLabels": {
          "fpp-manager.cennso.com/plumbing-pod-name": "{UPG-POD-NAME}",
          "fpp-manager.cennso.com/plumbing-ifname": "sxb"
        }
      }
    }
---
apiVersion: k8s.cni.cncf.io/v1
kind: NetworkAttachmentDefinition
metadata:
  name: upg-data-at1
  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": {
        "matchingLabels": {
          "fpp-manager.cennso.com/plumbing-pod-name": "{UPG-POD-NAME}",
          "fpp-manager.cennso.com/plumbing-ifname": "at1"
        }
      }
    }
---
apiVersion: k8s.cni.cncf.io/v1
kind: NetworkAttachmentDefinition
metadata:
  name: upg-data-de1
  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": {
        "matchingLabels": {
          "fpp-manager.cennso.com/plumbing-pod-name": "{UPG-POD-NAME}",
          "fpp-manager.cennso.com/plumbing-ifname": "de1"
        }
      }
    }
---
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": {
        "matchingLabels": {
          "fpp-manager.cennso.com/plumbing-pod-name": "{UPG-POD-NAME}",
          "fpp-manager.cennso.com/plumbing-ifname": "an"
        }
      }
    }

```

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.
