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

# Traffic capturing

There are three possible ways of capturing traffic on a VPP instance:

1. Using the VPPCTL CLI tool [trace](#capturing-using-trace) command
1. Using the VPPCTL CLI tool [pcap trace](#capturing-using-pcap-trace) command
1. Using a dedicated pod with the [SPAN interface](#span-interfaces)

>**CAUTION:** The `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 the workload pod that uses the TAP interface.

## Check the memory

To assess how much memory is left on the FPP pod, check main heap stats:

```bash
vppctl show memory main-heap
```

Output example:

```bash
Thread 0 vpp_main
  base 0x7f43b7299000, size 1g, locked, unmap-on-destroy, name 'main heap'
    page stats: page-size 4K, total 262144, mapped 235487, not-mapped 26657
      numa 0: 235487 pages, 919.87m bytes
    total: 1023.99M, used: 916.40M, free: 107.59M, trimmable: 107.14M
```

Heap stats are also exposed in these FPP metrics:

* `vpp_main_heap_total_bytes`
* `vpp_main_heap_used_bytes`
* `vpp_main_heap_free_bytes`

Measured the pcap size based on packet size and the number of packets to capture:

* 10k of UDP 64B packets -> ~20 MB
* 10k of UDP 594B packets -> ~60 MB
* 10k of UDP 1518B packets -> ~110 MB
* 1M of UDP 1518B packets -> ~900 MB

The memory taken from the heap does not return as free but is reused normally.
Due to VPP heap fragmentation, VPP stats (and corresponding metrics exposed by
the FPP pod) will not respond with released memory.

## Capturing using trace

You can enable text-based tracing of packets arriving on VPP interfaces using the command `vppctl trace`:

```bash
vppctl trace memif-input 10
```

In this case, 10 packets are captured. The trace is gathered on the VPP node. The possible values are:

- memif-input
- dpdk-input

To see all available options, go to the [official VPPCTL trace documentation](https://s3-docs.fd.io/vpp/22.10/gettingstarted/progressivevpp/traces.html#add-trace).

To see the gathered packets, run:

```bash
vppctl show trace
```

See the output example:

```
------------------- Start of thread 0 vpp_main -------------------
Packet 1

00:01:56:008214: memif-input
  memif: hw_if_index 1 next-index 4
    slot: ring 0
00:01:56:015712: ethernet-input
  IP4: 02:fe:0f:03:1d:0c -> 02:fe:61:70:88:98
00:01:56:015726: ip4-input
  ICMP: 10.99.99.2 -> 10.99.99.1
    tos 0x00, ttl 64, length 96, checksum 0xf974 dscp CS0 ecn NON_ECN
    fragment id 0xa65f
  ICMP echo_reply checksum 0x32b0 id 56479
00:01:56:015736: ip4-lookup
  fib 1 dpo-idx 13 flow hash: 0x00000000
  ICMP: 10.99.99.2 -> 10.99.99.1
    tos 0x00, ttl 64, length 96, checksum 0xf974 dscp CS0 ecn NON_ECN
    fragment id 0xa65f
  ICMP echo_reply checksum 0x32b0 id 56479
00:01:56:015744: ip4-receive
    ICMP: 10.99.99.2 -> 10.99.99.1
      tos 0x00, ttl 64, length 96, checksum 0xf974 dscp CS0 ecn NON_ECN
      fragment id 0xa65f
    ICMP echo_reply checksum 0x32b0 id 56479
00:01:56:015752: ip4-icmp-input
  ICMP: 10.99.99.2 -> 10.99.99.1
    tos 0x00, ttl 64, length 96, checksum 0xf974 dscp CS0 ecn NON_ECN
    fragment id 0xa65f
  ICMP echo_reply checksum 0x32b0 id 56479
00:01:56:015756: ip4-icmp-echo-reply
  ICMP4 echo id 56479 seq 1 send to cli node 698
```

To clear the trace, run:

```bash
vppctl clear trace
```

## Capturing using pcap trace

You can gather PCAPs from a specific VPP interface or from a VPP graph node.

- To gather packets from the VPP interface, use the [`pcap trace`](#pcap-trace) command.
- To gather traffic from a VPP graph node, use the [`pcap dispatch trace`](#pcap-dispatch-trace) command.

### pcap trace

To enable `pcap trace` on the memif interface, capturing both RX and TX directions, run this command in the FPP pod:

```bash
vppctl pcap trace rx tx drop max 1000 intfc memif1/101-grx26
```

- `1000` is the limit of captured packets
- `memif1/101-grx26` is the name of VPP interface on which the trace should be gathered.

For more details about the `pcap trace` command, read [the official VPP documentation](https://docs.fd.io/vpp/22.10/cli-reference/clis/clicmd_src_vnet.html#pcap-trace).

To see the status of PCAP capturing:

```bash
vppctl pcap trace status
```

Sample output:

```
pcap rx and tx dispatch capture enabled: 15 of 1000 pkts...
capture to file /tmp/rxtxdrop.pcap
```

To stop the trace, run:

```bash
vppctl pcap trace off
```

The PCAPs are stored in the `/tmp/rxtxdrop.pcap` file in the FPP pod. Copy this file from the pod to an external machine for further analysis. For example, you can use this command:

```bash
kubectl cp -n {FPP_NAMESPACE} {FPP_POD}:/tmp/rxtx.pcap ./trace-file.pcap
```

### pcap dispatch trace

The `pcap dispatch trace` command records VPP graph nodes that the packet goes through when it's processed. It allows you to see the path that the packet was steered or check why it was dropped. To activate the trace, you must set the observation point (VPP graph node):
- dpdk-input - used for physical interfaces
- memif-input - used for in-memory VPP interfaces

To enable `pcap dispatch trace`, run:

```bash
vppctl pcap dispatch trace on max 1000 buffer-trace dpdk-input 100
```

This gathers 1000 packets of DPDK traffic. The `100` value is required but ignored by VPP.

For more details about the `pcap dispatch trace` command, read the [official VPP documentation](https://s3-docs.fd.io/vpp/22.10/cli-reference/clis/clicmd_src_plugins_dispatch-trace.html).

To see the status of the dispatch trace, run:

```bash
vppctl pcap dispatch trace status
```

Sample output:

```
pcap dispatch capture enabled: 183 of 1000 pkts...
capture to file /tmp/dispatch.pcap
Buffer trace of 100 pkts from dpdk-input enabled...
```

To stop the trace, run:

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

This saves the PCAP file in `/tmp/dispatch.pcap` inside the FPP pod. To download it
to the local machine, use this command:

```bash
kubectl cp -n {FPP_NAMESPACE} {FPP_POD}:/tmp/dispatch.pcap ./vppcapture.pcap
```

### Traffic filtering

VPP allows filtering the packets before dumping them into the PCAP file. To do so, first create the classifier:

```bash
vppctl classify filter pcap mask l3 ip4 src dst match l3 ip4 src 192.168.1.10 dst 192.168.2.10
```

After creating the classifier, pass `filter` at the end of the `pcap trace` command, for example:

```bash
vppctl pcap trace rx tx drop max 1000 intfc memif1/101-grx26 filter
```

For more details about the `classify filter` command, read the [official VPP documentation](https://docs.fd.io/vpp/22.02/cli-reference/clis/clicmd_src_vnet_classify.html).

## SPAN interfaces

For FPP-based CNFs, traffic can be captured using the SPAN functionality of FPP.
This approach is mandatory for VPP-based CNFs that use the `memif` interface type on FPP.
You can find more details in the [fpp-manager documentation](https://docs.cennso.com/components/fpp/3.2/operation-guides/span-config.md).

To capture traffic from an FPP interface, a dedicated pod with the `pcap-collector` container can be started.
A target FPP interface is attached to the pod, which receives the SPAN traffic of a defined FPP source interface.

The respective `NetworkAttachmentDefinition` (created on the Core Cluster) looks like this:

```yaml
apiVersion: k8s.cni.cncf.io/v1
kind: NetworkAttachmentDefinition
metadata:
  name: span-sxb
  namespace: fpp-sample
spec:
  config: |-
    {
      "cniVersion": "0.3.0",
      "ipam": {},
      "mtu": 9000,
      "name": "tst",
      "type": "fpp-cni",
      "vni": 1,
      "vppIfaceType": "tap",
      "spanSource": {
        "matchingLabels": {
          "fpp-manager.cennso.com/plumbing-pod-name": "cnf-1",
          "fpp-manager.cennso.com/plumbing-ifname": "sxb"
        }
      }
    }
```

If the CNI-less feature is used, you must define **vppInterfaceRef** in the **spanSource** section, with the **name** and the **namespace** of the slave memif `VPPInterface` CR. See an example of `NetworkAttachmentDefinition` (created on the Core Cluster):

```yaml
apiVersion: k8s.cni.cncf.io/v1
kind: NetworkAttachmentDefinition
metadata:
  name: span-sxb
spec:
  config: |-
    {
      "cniVersion": "0.3.0",
      "ipam": {},
      "mtu": 9000,
      "name": "span-upg-sx",
      "type": "fpp-cni",
      "vni": 1,
      "vppIfaceType": "tap",
      "spanSource": {
        "vppInterfaceRef": {
          "name": "upg-sample-sxb",
          "namespace": "upg-test"
        }
      }
    }
```

The pod must refer to the created resource through an annotation in order to create an `sxb` interface where the mirrored traffic can be captured:

```yaml
spec:
  template:
    metadata:
      annotation:
        k8s.v1.cni.cncf.io/networks: span-sxb@sxb
```

In order not to send the mirrored traffic across nodes through the distributed switch domain,
pin the capturing pod to the same node as the CNF is running on.

```yaml
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                - worker-dataplane-2001
```

To avoid bogus traffic being sent from the capturing pod through the Kubernetes pod network,
ensure that no main routing table applies, for example, by using the following script:

```yaml
      initContainers:
        image: quay.io/travelping/nettools:v1.10.1
        name: network-init
        command:
        - /bin/sh
      - args:
        - -c
        - |
          set -x
          ip rule add iif vni1 lookup 100
          ip route add blackhole default table 100
```

## Tracing commands

These are the other useful VPP CLI commands for traffic capturing:

* `trace add memif-input 20` - enable text-based tracing of packets arriving on memif interfaces
  (that is, packets coming from VPP apps in case of VPPSwitch). Suitable during very low traffic
* `trace add virtio-input 20` - enable text-based tracing of packets arriving on tap interfaces
  (that is, packets coming from Linux networking pods in case of VPPSwitch). Suitable during
  very low  traffic
* `trace add dpdk-input 20` - enable text-based tracing of packets arriving on uplinks
* `show trace` - show text-based trace
* `clear trace` - clear and disable text-based trace
* `pcap trace tx rx max 10000 intfc memif1/100 file foo.pcap` - enable
  PCAP capture in both RX and TX directions, writing to
  `/tmp/foo.pcap` inside the pod. Will capture up to 10000
  packets. Note that the whole trace is stored in RAM and thus cannot
  be unlimited.
* `pcap trace off` - disable tracing and write the capture file
* `pcap dispatch trace on max 10000 file vppcapture buffer-trace memif-input 100` - enable dispatch
  trace that stores VPP graph node info inside the PCAP, max 10000 packets,
  and write it to `/tmp/vppcapture` inside the pod
* `pcap dispatch trace off` - disable dispatch tracing and write the capture file
