> 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/memcap.md).

# MEMCAP traffic capturing

You can use the MEMCAP plugin for traffic capturing.

MEMCAP is a framework that defines and implements a protocol for offloading the traffic capturing process using the memory-mapped interface. This approach reduces the operational overhead on the capturing component. Captured data is subsequently collected and processed by the MEMCAP Sink sub-component. By delegating resource-intensive tasks, such as I/O processing and system upgrades, to separate components, MEMCAP reduces resource requirements and improves scalability.

The Fast Path Provider (FPP) pod acts as a traffic collector and communicates with the MEMCAP Sink pod using a shared memory interface. Multiple FPP-based instances, such as FPP, Connectivity Gateway (CGW) or User Plane Gateway (UPG), located on a single node communicate with a single MEMCAP Sink pod located on the same node.

![MEMCAP diagram](https://docs.cennso.com/api/assets/components/fpp/1.3/assets/memcap-diagram.svg)

## Terminology

Here's the terminology used within this document:

- Collector - FPP pod which provides capture streams. It can provide multiple PCAP streams in parallel.
- Sink - a component that receives data from collectors, processes it, and outputs as `.pcapng` files.
- Class - a traffic class which allows for different processing in MEMCAP Sink. For example, `hw_iface_l2` is a VPP interface-based L2 traffic capturing, and `upf_session_ip` is a UPG IMSI-based IP traffic capturing.
- Target - an arbitrary name used to direct traffic capture streams to destinations. On the collector side, different traffic capture streams can use the same target name. On the MEMCAP Sink side, different destinations can use the same target name as well. Target names can be chosen arbitrarily and do not require provisioning in advance.
- Stream - an instance of active traffic capture. For example, a stream of interface `eth0` to target `eth-if` with class `hw_iface_l2`.
- Destination - the endpoint for a PCAP stream, which can be a file or an HTTP stream.

MEMCAP consists of the following sub-components:

- **VPP MEMCAP plugin** implements the collector side of the MEMCAP connection. It implements the `hw_iface_l2` collector, which allows you to collect traffic from L2 VPP interfaces. It also provides a framework for easy integration with other VPP plugins.

- **MEMCAP Sink** manages connections to collectors and processes the captured traffic data. It outputs the data in the `.pcapng` file format, which you can save to a disk with options for compression and rotation. Additionally, it allows streaming of `.pcapng` files over an HTTP connection for real-time analysis.

This is a sample operational workflow:

![MEMCAP flow](https://docs.cennso.com/api/assets/components/fpp/1.3/assets/memcap-flow.svg)

## Installation

MEMCAP Sink is installed as a regular Helm chart. To install it, use the following command:

```bash
helm upgrade --install memcap-sink cennso/memcap-sink -f ./values/memcap.yaml  -n fpp --version 0.3.0
```

The `-n fpp` part of the command specifies a namespace where the MEMCAP sink will be deployed. For more details about the `memcap.yaml` file, read the Configuration section below.

## Configuration

You can configure MEMCAP Sink using Helm chart configurable parameters. Such configuration allows you to:
- Name a PCAP file
- Enable file rotation
- Define how many packets are stored in a single file
- Define the maximum number of files
- Enable file compression

These are the parameters that you can configure:

| Key | Type | Default | Description |
|---|---|---|---|
| annotations | object | `{}` | Additional annotations for all resources except for the DaemonSet's pods. |
| imagePullSecrets | list | `[]` | `imagePullSecrets` used for pulling the images. Sample value: `[{name: "regcred"}]`. |
| labels | object | `{}` | Additional labels for all resources except for the DaemonSet's pods |
| memcapSink.affinity | object | `{}` | Assigns custom affinity rules to MEMCAP Sink. For more information, read the official [Kubernetes documentation](https://kubernetes.io/docs/concepts/configuration/assign-pod-node/). |
| memcapSink.enabled | bool | `true` | Enables the MEMCAP Sink DaemonSet deployment |
| memcapSink.envs | list | `[]` | Custom environment variables for the MEMCAP container. Sample value: `[{"name":"APP_EXAMPLE_NAME","value":"APP_EXAMPLE_VALUE"},{"name":"SECRET_USERNAME","valueFrom":{"secretKeyRef":{"name":"mysecret","key":"username","optional":false}}}]` |
| memcapSink.hostSocketDirectory | string | `"/var/run/memcap"` | Host directory used for communication with collectors |
| memcapSink.image.pullPolicy | string | `"IfNotPresent"` | Image pull policy. The possible values are: `Always`, `IfNotPresent`, and `Never`. |
| memcapSink.image.repository | string | `"registry.cennso.com/cennso/memcap-sink"` | Repository where the MEMCAP Sink's image is stored |
| memcapSink.image.tag | string | `""` | Overrides the image tag, which defaults to the chart appVersion. |
| memcapSink.logLevel | string | `"info"` | Log level. The possible values are: `panic`, `fatal`, `error`, `warn`, `info`, `debug`, and `trace`. |
| memcapSink.nodeSelector | object | `{}` | Selects nodes which the MEMCAP Sink should run. For more information about nodeSelectors, read the official [Kubernetes documentation](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#nodeselector). |
| memcapSink.resources.cpu | string | `"500m"` | CPU assigned to the MEMCAP container. For more information about nodeSelectors, read the official [Kubernetes documentation](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/). |
| memcapSink.resources.memory | string | `"256Mi"` | Memory assigned to the MEMCAP container. For more information about nodeSelectors, read the official [Kubernetes documentation](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/). |
| memcapSink.tolerations | list | `[]` | Kubernetes tolerations. For more information, read the official [Kubernetes documentation](https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/). |
| podAnnotations | object | `{}` | Additional annotations for pods |
| podLabels | object | `{}` | Additional labels only for pods |
| memcapSink.destinations | list | `[]` | List of static destinations |
| memcapSink.destinations.[].targets | list | `[]` | List of target names attached to this destination |
| memcapSink.destinations.[].pcap.file_prefix | string | `""` | Prefix for autogenerated file names |
| memcapSink.destinations.[].pcap.rotate | bool | `false` | Enables file rotation |
| memcapSink.destinations.[].pcap.max_packets | int | `0` | Packets count limit for triggering file rotation |
| memcapSink.destinations.[].pcap.max_bytes | int | `0` | Packets total size limit for triggering file rotation |
| memcapSink.destinations.[].pcap.max_duration | duration | `0m0s` | Time limit for triggering rotation |
| memcapSink.destinations.[].pcap.max_files | int | `0` | Total files count limit for file rotation |
| memcapSink.destinations.[].pcap.compressed | bool | `false` | Enables compression for files |

See the MEMACAP sink configuration example:

```yaml
imagePullSecrets:
- name: cur-secret
memcapSink:
  logLevel: info
  destinations:
  - targets: [pgw-pfcp, scg-pfcp]
    pcap:
      file_prefix: "/root/pfcp"
      rotate: true
      max_packets: 100000
      max_files: 10
      compressed: true
  tolerations:
  - effect: NoSchedule
    key: node-role.cennso.net/dataplane
    value: "true"
```

This configuration creates a single PCAP file for two targets (`pgw-pfcp` and `scg-pfcp`). The PCAP file is stored in the MEMCAP pod in the location specified in the **file_prefix** field. A file name always contains an autogenerated suffix to avoid collisions during rotations or restarts. You can copy a PCAP file from the pod to an external machine for further analysis. For example, you can use this command:

```bash
kubectl cp -n fpp memcap-pod:/root/pfcp_1717531911.911109.pcapng.zstd ./trace-file.pcapng.zstd
```

## Usage

### Enable traffic capturing

To initiate traffic capturing on a specific interface, use this command:
```
vppctl set interface memcap ${INTERFACE_NAME} target ${TARGET_NAME} [filter {tcpdump filter}] [packet_max_bytes 9000] [rx] [tx]
```
- `filter {tcpdump filter}` - allows you to define libpcap-based filters that are the same as in the tcpdump utility. The filter format is described in the libpcap documentation `man pcap-filter`. Examples: `filter {arp or icmp}`, `filter {src net 192.0.2.0/24 and port 2020}`.
- `packet_max_bytes` is an optional parameter which allows you to limit the size of collected data to reduce memory usage of a traffic capture
- `rx` and `tx` allow you to specify the direction in which traffic should be collected. Leaving those flags out collects traffic in both directions.

Note that you can create multiple captures for the same interface when using different capture targets. This is useful if you want to simultaneously collect differently filtered traffic to separate files from a single interface.

Each active stream on the interface may slow down the entire interface performance. To reduce performance degradation, it's best to keep the number of streams attached to interfaces low. This can be done by combining filters using the libpcap `or` operator.

### Disable traffic capturing

To disable traffic capturing on a specific interface, use this command:
```
vppctl set interface memcap ${INTERFACE_NAME} target ${TARGET_NAME} disable
```

### Monitor traffic capture

To view the created interface captures, use the command:
```
vppctl show interface memcap
```
See the output example:
```
Interface "left" [1] streams: 1
- target "dp" max_bytes 100 memcap id 3 rx tx jit_fn 0x0
Interface "right" [2] streams: 1
- target "dp" max_bytes 100 memcap id 2 rx tx jit_fn 0x0
Interface "sx" [3] streams: 3
- target "sx" max_bytes 9000 memcap id 0 rx tx jit_fn 0x7ffff7ffb000
  filter: {arp or icmp}
- target "sx-ipv6" max_bytes 9000 memcap id 1 tx jit_fn 0x7ffff7fc8000
  filter: { ip6 }
- target "sx-ipv4" max_bytes 9000 memcap id 8 rx tx jit_fn 0x7ffff7fc7000
  filter: { ip }
```

To view all the created MEMCAP streams from all plugins, use the command:
```
vppctl show memcap streams
```
See the output example:
```
DBGvpp# show memcap streams
Streams: 7
 [0] "sx" class: [0]hw_iface_l2 target: "sx" state: stopped
 [1] "left" class: [0]hw_iface_l2 target: "dp" state: stopped
 [2] "right" class: [0]hw_iface_l2 target: "dp" state: stopped
 [3] "imsi 001010100000000" class: [1]upf_session_ip target: "ue-1" state: stopped
```

To view MEMCAP connection state, use the command:
```
vppctl show memcap status
```
See the output example:
```
DBGvpp# show memcap status
Name: "unnamed memcap v0.3.0"
Connection:
  Path: /var/run/memcap/sink.sock
  State: sink active
  FD: 7
  Remote Name: "sink test example"
  Memory region
    Size: 0x201000
    Address: 0x81a01000
    MemFD: 16
Registered classes:
  [0] hw_iface_l2 (packet metadata: 0, stream metadata: 4)
  [1] upf_session_ip (packet metadata: 0, stream metadata: 0)
```

## Capture information

You can use the additional metadata provided in a `.pcapng` file for advanced filters in tools like Wireshark:

| Name | Description |
|:---|:---|
| `frame.interface_name` | Interface name. |
| `frame.interface_description` | Additional information that includes a target name. |
| `frame.interface_queue` | Worker index where packet capture happened. |
| `frame.packet_flags_direction` | Direction of a packet relative to VPP. |
| `frame.time` | Time of packet capture. Multiple packets can have the same capture time when they share the same vector in VPP. |
| `frame.cap_len` | Capture length. It can be different from packet length when additional capture limiter parameters are provided. |
| `frame.drop_count` | Number of dropped packets for the packet interface. This field is optional. It exists when slow IO causes packet loss. |

## Simultaneous streaming through HTTP

Currently, MEMCAP Sink does not support simultaneous streaming from multiple nodes through a single connection. For simultaneous streaming on multiple nodes, you must repeat these steps for every node:

1. Set up port forwarding to one of MEMCAP Sink pods. The pod must be located on the node where you want to capture traffic.

   ```bash
   kubectl port-forward pod/memcap-memcap-sink-xyzwq 9999:9999
   ```

   This command maps port `9999` on your local machine, allowing direct access to the streaming service.

2. To start streaming the `.pcapng` file to Wireshark, create an HTTP request and redirect it as a stream to Wireshark. You can specify multiple target names in the URL, separated by commas, to select the traffic capture as required.

   ```bash
   curl --compressed localhost:9999/pcap/TARGET1,TARGET2,TARGET3 -N | wireshark -k -i - -o 'gui.window_title:memcap-stream' -o 'capture.no_interface_load:TRUE'
   ```

   This command performs the following actions:
   - `curl --compressed` - starts a cURL and requests a server to perform the request compression and decompression before writing data to a pipe.
   - `localhost:9999/pcap/TARGET1,TARGET2,TARGET3` - URL to the forwarded MEMCAP Sink API port with a comma-separated list of target names.
   - `-N` - disables pipe buffering to ensure real-time updates in Wireshark.
   - `| wireshark -k -i -` - sends (pipes) the output from curl directly into Wireshark, which starts analyzing the stream immediately.
   - `-o 'gui.window_title:memcap-stream'` - sets the Wireshark window title for easy identification.
   - `-o 'capture.no_interface_load:TRUE'` - prevents Wireshark from listing local interfaces, which speeds up the startup.

As a result, you should see live traffic in Wireshark.
