> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/upg/latest/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/latest/deployment-guides/multithreading.md).

# Multithreading

User Plane Gateway (UPG) supports multithreading. When multithreading is enabled, Vector Packet Processing (VPP) runs one main thread plus multiple worker threads that process packets in parallel across CPU cores.

## Enable multithreading

To enable multithreading, set **spec.vpp.multicore** to `true` in the `UPGDeployment` CR:

```yaml
spec:
  vpp:
    multicore: true
```

In multicore mode, VPP allocates one core for the main thread and uses the remaining cores available to the pod as worker cores.

Three fields control how many cores VPP uses. The following table lists them in the order VPP resolves them:

| Field | Effect |
| --- | --- |
| **spec.vpp.staticCPUCores** | Pins VPP to an explicit core list, for example `"1,2,3-5"`. The first core runs the main thread and every other core runs a worker. Set it together with `isolcpus=` on the host kernel command line. |
| **spec.vpp.maxCPUCores** | Caps the number of cores VPP uses, including the main thread. For example, `maxCPUCores: "4"` gives one main thread and three workers. VPP drops the cap when the pod has fewer usable cores than the cap asks for. |
| **spec.resources.cpu** | Sets the CPU that Kubernetes gives the pod. This is the upper bound on the cores VPP can use. |

Kubernetes applies **spec.resources.cpu** as both the CPU request and the CPU limit. That allocation bounds **spec.vpp.maxCPUCores**, and a cap cannot raise the thread count above the allocation. The [Thread allocation](https://docs.cennso.com/components/upg/latest/observability/performance.md#thread-allocation) table lists how many threads each allocation yields.

**spec.vpp.staticCPUCores** overrides the other two fields. When you set it, VPP ignores **spec.vpp.maxCPUCores** and **spec.vpp.multicore**, and the core list alone decides the thread count. The pod then runs without a CPU request or limit, so **spec.resources.cpu** has no effect. Without CPU resources, the VPP container has no cgroup cpuset limit.

On a host with hyper-threading, **spec.vpp.allowHtSiblings** decides how much CPU each thread costs. With the default `false`, a VPP thread takes a whole physical core, which is two logical CPUs on a hyper-threaded host. With `true`, a thread takes a single sibling, so the same number of threads fits in half the physical cores.

The two settings suit different limits. Set it to `true` when physical cores are scarce, because the deployment then reaches a given thread count on half of them. Leave it at the default when each thread has to carry as much as the host can give, because a thread that owns a whole core carries more than one that shares it. [Performance](https://docs.cennso.com/components/upg/latest/observability/performance.md#hyper-threading-siblings) lists both sides of that trade.

> [!IMPORTANT]
> **spec.vpp.allowHtSiblings** is intended for testing only. The Fast Path Provider (FPP) `VPPDeployment`, `FPPDeployment`, and `CGWDeployment` schemas mark their own `allowHtSiblings` field the same way. The benchmark ran UPG on siblings without errors, and [Performance](https://docs.cennso.com/components/upg/latest/observability/performance.md#hyper-threading-siblings) records the throughput it measured.

## Sizing

The main thread forwards traffic only while it is the only thread. As soon as one worker exists, the main thread becomes control-plane only, and all forwarding moves to the workers. One main thread plus one worker therefore still gives a single forwarding thread. That costs twice the CPU of a main-only deployment, with no throughput gain.

Provision either a main-only deployment or at least two workers, and skip the single-worker configuration in between. The [Performance](https://docs.cennso.com/components/upg/latest/observability/performance.md) page records the throughput measured for each configuration.

## Auto memif queue placement

When you enable multithreading, auto placement configures the receive (Rx) and transmit (Tx) queues on the following in-memory interfaces (memifs):

- Access Network interface (GRX)
- Data Network interface (SGi)
- Network Address Translation (NAT) pool interfaces

Auto placement sets the number of queues and distributes them across the worker threads, so each worker handles a share of the traffic.

Each of these interfaces connects UPG to the VPPSwitch pod of FPP. On each side, auto placement adds the main thread to Tx queue 0, because the main thread sends some packets itself. On the UPG side, these packets include:

- Address Resolution Protocol (ARP) probes
- Neighbor Discovery (ND) probes
- IP Flow Information Export (IPFIX) templates

### Disable auto placement for a single interface

To override auto placement on a specific interface, set any of the following fields under that interface in **spec.instances.{name}.networking.{interface}**:

- **numRxQueues** sets the number of Rx queues.
- **numTxQueues** sets the number of Tx queues.
- **txPlacement** sets the Tx queue placement on the VPPSwitch side. VPPSwitch sends on the queues that UPG receives on, so the array has one entry for each of the **numRxQueues** queues. Each entry is a comma-separated list of the VPPSwitch threads that send on that queue.
- **clientTxPlacement** sets the Tx queue placement on the UPG side. The array has one entry for each of the **numTxQueues** queues. Each entry is a comma-separated list of the UPG threads that send on that queue.

If you set any of these fields on an interface, UPG skips auto placement for that interface and uses only your values.

Thread 0 is the main thread, and threads 1 and higher are the workers. Each thread that sends packets on the interface needs a Tx queue, including thread 0. In **txPlacement** and **clientTxPlacement**, add thread 0 to the list of queue 0, as auto placement does. On a side without workers, use `"0"` for every queue.

> [!CAUTION]
> If no list includes thread 0, VPP discards every packet from the main thread on the interface. VPP counts these packets as `no tx queue available`. For example, the ARP and ND probes from the main thread do not reach the neighbors, so VPP removes the neighbor entries. VPP then drops the packets to each neighbor until VPP resolves the neighbor again.

The following example configures the SGi interface for two workers on each side:

```yaml
spec:
  instances:
    inst01:
      networking:
        sgi:
          interface: sgi
          networkName: sgi41
          numRxQueues: 2
          numTxQueues: 2
          txPlacement: ["0,1", "2"]
          clientTxPlacement: ["0,1", "2"]
```

On each side, threads 0 and 1 share queue 0, and thread 2 uses queue 1.

### Disable auto placement globally

To disable auto queue placement on all dataplane interfaces, set **spec.vpp.queuesFastAutoAllocation** to `false`:

```yaml
spec:
  vpp:
    multicore: true
    queuesFastAutoAllocation: false
```

When auto placement is disabled globally, every dataplane interface uses the default single-queue configuration unless you override it with the fields listed in [Disable auto placement for a single interface](#disable-auto-placement-for-a-single-interface).

## Verification

Check the running threads and per-interface queue placement with `vppctl` in the UPG pod.

1. List the VPP threads and check that they match the configuration:

   ```bash
   vppctl show threads
   ```

   Sample output:

   ```txt
   ID     Name                Type        LWP     Sched Policy (Priority)  lcore  Core   Socket State
   0      vpp_main                        23      other (0)                0      0      0
   1      vpp_wk_0            workers     24      other (0)                1      4      0
   2      vpp_wk_1            workers     25      other (0)                2      16     0
   3      vpp_wk_2            workers     26      other (0)                3      20     0
   4      vpp_wk_3            workers     27      other (0)                4      24     0
   ```

   One `vpp_main` thread plus one `vpp_wk_N` per worker. Each thread is pinned to a host CPU (`lcore`), and `Core` is its physical core ID.

2. Check the Rx queue placement across worker threads on all interfaces:

   ```bash
   vppctl show interface rx-placement
   ```

   Sample output:

   ```txt
   Thread 1 (vpp_wk_0):
    node memif-input:
       sxb queue 0 (polling)
       sgi queue 3 (polling)
       grx queue 3 (polling)
   Thread 2 (vpp_wk_1):
       sgi queue 0 (polling)
       grx queue 0 (polling)
   Thread 3 (vpp_wk_2):
       sgi queue 1 (polling)
       grx queue 1 (polling)
   Thread 4 (vpp_wk_3):
       sgi queue 2 (polling)
       grx queue 2 (polling)
   ```

   Each dataplane interface queue is pinned to a single worker thread.

3. Inspect the memif Rx/Tx queue counts on a specific dataplane interface:

   ```bash
   vppctl show memif sgi
   ```

   Replace `sgi` with the interface name configured in **spec.instances.{name}.networking.{interface}.interface**.

   Sample output:

   ```txt
   sockets
     id  listener    filename
     0   no          /run/vpp/memif.sock
     1   no          /run/vppswitch/memif.sock

   interface sgi
     remote-name "VPP 24.02.0-47~gd1590c8bb"
     remote-interface "memif1/101"
     socket-id 1 id 101 mode ethernet
     flags admin-up slave connected zero-copy
     num-s2m-rings 4 num-m2s-rings 4 buffer-size 2048 num-regions 2
     ...
   ```

   `num-s2m-rings` and `num-m2s-rings` show the configured Rx and Tx queue counts on the interface.

When all checks pass, the threads and queues match the configuration.
