# Media Node Tuning and Diagnostics

Host-level tuning and diagnostics for eduMEET media nodes, beyond the basic deployment in the [federation guide](<https://hifis.net/doc/cloud-services/Letsmeet_DKFZ/federation/index.md>). Apply the buffer tuning before going live; use the diagnostics when participants report frozen video or dropped streams.

## Symptom: Frozen Video

WebRTC media flows over UDP in bursts — a screen share or several cameras easily push hundreds of packets per second through a single socket. If the kernel’s UDP socket buffers are too small, the kernel drops packets silently: the receiving decoder stalls until the next keyframe arrives, and participants see frozen video. The indicator is the kernel-wide `RcvbufErrors` counter:

```
awk '$1=="Udp:" && $2 ~ /^[0-9]+$/{printf "InDG %s | NoPorts %s | InErr %s | OutDG %s | RcvBufErr %s | SndBufErr %s\n",$2,$3,$4,$5,$6,$7}' /proc/net/snmp
```

## UDP Socket Buffers

The Linux defaults (`net.core.rmem_default` ≈ 208 KB) are too small for a busy media node. Raise them **on the host** — the container shares the host network stack and the media node does not set its own buffer sizes:

```
sudo tee /etc/sysctl.d/99-edumeet-webrtc.conf >/dev/null <<'EOF'
net.core.rmem_default = 4194304
net.core.rmem_max = 16777216
net.core.wmem_default = 4194304
net.core.wmem_max = 16777216
EOF
sudo sysctl --system
```

- WebRTC transports open their UDP sockets dynamically when a room is used — new sockets inherit the new defaults **without a container restart**. Sockets that already exist (long-running rooms) keep their old size until they are closed.
- A hardened production baseline additionally raises the kernel backlog queue and the UDP memory thresholds. The `udp_mem` values are in 4 KiB pages and scale with host RAM — compare against `sysctl -n net.ipv4.udp_mem` before applying:

  ```
  net.core.netdev_max_backlog = 16384
  net.core.netdev_budget = 600
  net.core.netdev_budget_usecs = 8000
  net.ipv4.udp_mem = 65536 131072 262144
  net.ipv4.udp_rmem_min = 16384
  net.ipv4.udp_wmem_min = 16384
  ```

## Verifying per-Socket Buffers

Each WebRTC transport owns one UDP socket in the configured RTC port range. `ss -uanmp` shows the socket line followed by an indented `skmem:(...)` line — `rb` is the effective buffer limit, `d` the drop counter:

```
ss -uanmp | grep -A1 -E ':(402[5-9][0-9]|40[34][0-9][0-9])'
```

| Field | Meaning | Healthy value |
| --- | --- | --- |
| `RecvQ` (second column of the socket line) | bytes received but not yet read by the worker | 0; small transient values under load are normal sampling artifacts |
| `r` | bytes currently sitting in the receive buffer | \~0 |
| `rb` | receive buffer limit (= `net.core.rmem_default`) | 4194304 after tuning |
| `d` | packets dropped on this socket | 0 |

Keep two properties of these counters in mind:

- `d` is **cumulative over the socket’s lifetime** — it never resets while the socket exists. A small value that stays constant across refreshes is a historic single event that WebRTC repaired invisibly (NACK/RTX); only a value that grows from refresh to refresh is a problem.
- `RecvQ` is a snapshot taken at the moment of the query. Values of a few KB are normal micro-bursts (a handful of RTP packets ≈ 20 ms of media) — treat a *persistent* RecvQ above \~64 KB as the warning sign before drops start.

Rule of thumb: single dropped packets are repaired invisibly; a lost keyframe fragment freezes the video until the next keyframe (0.2–1 s); a sustained loss rate freezes it permanently. Occasional counter movements of one or two are cosmetic — sustained rates are the problem to fix.

## Diagnostics Quick Reference

| Command | What to look at |
| --- | --- |
| `awk '$1=="Udp:" && $2 ~ /^[0-9]+$/' /proc/net/snmp` | kernel-wide UDP counters; `RcvbufErrors` and `SndbufErrors` must stay flat (column order shown above) |
| `ss -uanmp` | per-socket `rb`/`d` (see above) |
| `cat /proc/net/softnet_stat` | per-CPU values in hex: 2nd column = dropped packets, 3rd = time squeezes; any increase means the kernel network queues overflow before packets reach the sockets |
| `ip -s link` | interface counters; on bonded hosts, bond0 drops that track the multicast counter are harmless — drops on the **slave** interfaces must stay 0 |
| `top -H -p $(pgrep -d, mediasoup-worke)` | no mediasoup worker thread should sit at 100 % CPU continuously (the process name is truncated to 15 characters, hence `mediasoup-worke`) |

## Bonded Interfaces (LACP)

If the host uses bonded NICs in 802.3ad mode, the default `xmit_hash_policy` is `layer2` — MAC-based hashing pins all egress traffic to a single slave, because media always flows to the same gateway MAC. Switch to `layer3+4` so streams distribute across both links:

```
cat /sys/class/net/bond0/bonding/xmit_hash_policy   # check — layer2 = unbalanced
echo "layer3+4" | sudo tee /sys/class/net/bond0/bonding/xmit_hash_policy >/dev/null   # live, no link flap
```

Persist the setting in the interface configuration (e.g. `bond-xmit-hash-policy layer3+4` under `iface bond0` in `/etc/network/interfaces`). The inbound distribution is decided by the switch, not by the host — only the transmit side can be judged.

## Debug Logging

Keep the media node’s debug setting at `mediasoup:WARN*` (as in the compose file above). Running with `DEBUG="*"` produces heavy synchronous logging that blocks the worker event loop — it *causes* exactly the freezes you would be debugging.

---
