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

# Traffic capturing

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

To assess how much memory is left on the VPP pod, run main heap stats:

```console
$ vppctl -s /run/vppswitch/cli.sock show memory main-heap
```

Output example:

```console
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 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 manager) will not respond with released memory.

## Capturing on physical NICs

Traffic handled by physical NICs is passed using the `dpdk-input` VPP node. To enable
`dispatch trace` which records VPP graph node packet processing info, run:

```console
$ vppctl -s /run/vppswitch/cli.sock pcap dispatch trace on max 1000 file vppcapture.pcap buffer-trace dpdk-input 1000
```

This gathers 1000 packets max on DPDK traffic. To stop the trace, run:

```console
$ vppctl -s /run/vppswitch/cli.sock pcap dispatch trace off
```

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

```console
$ kubectl cp -n <fpp-ns> <fpp-pod>:/tmp/vppcapture.pcap ./vppcapture.pcap
```

## Capturing on pod

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

```console
$ vppctl -s /run/vppswitch/cli.sock pcap trace rx tx max {NUM} intfc memif1/101-grx26
```

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

When the trace should be stopped, run:

```console
$ vppctl -s /run/vppswitch/cli.sock pcap trace off
```

The pcap will be stored in the `/tmp/rxtx.` file in the FPP pod. For further analysis,
this file needs to be copied from the pod to an external machine by hand,
for example by this command:

```console
$ kubectl cp -n <fpp-ns> <fpp-pod>:/tmp/rxtx.pcap ./trace-file.pcap
```

## 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
