Skip to content

Commit 20dc8ee

Browse files
committed
fix: swap TAP rx/tx for correct VM perspective
TAP interface statistics are from the host's perspective: - TAP rx_bytes = bytes host receives = bytes VM transmits - TAP tx_bytes = bytes host transmits = bytes VM receives The API documents these as "bytes received/transmitted by the VM", so we need to swap them to match the VM's perspective.
1 parent 6b3d6fc commit 20dc8ee

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/vm_metrics/collector.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ func ReadProcStatm(pid int) (rssBytes, vmsBytes uint64, err error) {
9393

9494
// ReadTAPStats reads network statistics from a TAP device.
9595
// Reads from /sys/class/net/<tap>/statistics/{rx,tx}_bytes.
96+
// Note: Returns stats from host perspective. Caller must swap for VM perspective:
97+
// - rxBytes = host receives = VM transmits
98+
// - txBytes = host transmits = VM receives
9699
func ReadTAPStats(tapName string) (rxBytes, txBytes uint64, err error) {
97100
basePath := filepath.Join("/sys/class/net", tapName, "statistics")
98101

lib/vm_metrics/manager.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,11 @@ func (m *Manager) GetInstanceStats(ctx context.Context, info InstanceInfo) *VMSt
8989
if err != nil {
9090
log.DebugContext(ctx, "failed to read TAP stats", "instance_id", info.ID, "tap", info.TAPDevice, "error", err)
9191
} else {
92-
stats.NetRxBytes = rxBytes
93-
stats.NetTxBytes = txBytes
92+
// TAP stats are from host perspective, swap for VM perspective:
93+
// - TAP rx_bytes = host receives = VM transmits
94+
// - TAP tx_bytes = host transmits = VM receives
95+
stats.NetRxBytes = txBytes
96+
stats.NetTxBytes = rxBytes
9497
}
9598
}
9699

0 commit comments

Comments
 (0)