[PR #6419] [CLOSED] feat(amneziawg): native kernel module acceleration with automatic userspace fallback #6261

Closed
opened 2026-11-02 04:07:29 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6419
Author: @rqzbeh
Created: 9/5/2026
Status: ❌ Closed

Base: main ← Head: feat/amneziawg-kernel-module-engine


📝 Commits (6)

  • 2b62cbb feat(amneziawg): add kernel module detection and probe helper
  • c927a45 feat(amneziawg): add kernel module detection and preflight checks
  • b3af34d feat(amneziawg): abstract engine interface with automatic fallback in manager
  • 575e61b feat(amneziawg): implement native kernel module engine with routing lifecycle
  • abea642 docs(amneziawg): document kernel module acceleration and docker setup
  • a9e1384 fix(amneziawg): resolve golangci-lint linter issues in kernel routing

📊 Changes

11 files changed (+700 additions, -17 deletions)

View changed files

📝 docs/content/docs/en/config/amneziawg.mdx (+42 -8)
📝 internal/amneziawgnet/diagnostics.go (+3 -6)
➕ internal/amneziawgnet/engine.go (+15 -0)
➕ internal/amneziawgnet/engine_fallback_test.go (+61 -0)
➕ internal/amneziawgnet/kernel_detect.go (+64 -0)
➕ internal/amneziawgnet/kernel_detect_test.go (+141 -0)
➕ internal/amneziawgnet/kernel_engine.go (+137 -0)
➕ internal/amneziawgnet/kernel_engine_test.go (+22 -0)
➕ internal/amneziawgnet/kernel_routing_linux.go (+154 -0)
➕ internal/amneziawgnet/kernel_routing_other.go (+29 -0)
📝 internal/amneziawgnet/manager.go (+32 -3)

📄 Description

Summary

Adds native Linux kernel module acceleration (amneziawg-linux-kernel-module) for AmneziaWG inbounds with automatic, seamless fallback to the embedded amneziawg-go + gVisor userspace engine when unsupported or unprivileged.

Why

While the embedded amneziawg-go engine provides 100% portability without host kernel dependencies, high-bandwidth deployments benefit significantly from zero-copy native kernel processing. This dual-engine architecture probes for the kernel module at runtime:

  • If the Linux host has amneziawg.ko loaded and the process has CAP_NET_ADMIN, 3x-ui manages native awg<id> interfaces and routes peer traffic via policy routing / TPROXY into Xray, preserving full Xray routing rules, outbounds, sniffing, and live per-client accounting.
  • If the kernel module is absent, unprivileged (e.g. standard unprivileged Docker), or on non-Linux platforms, 3x-ui automatically falls back to the embedded userspace engine with zero configuration changes.

Type of change

  • New feature

Areas affected

  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Documentation

How was this tested?

  • Unit tests for preflight detection (DetectKernelSupport and IsKernelModuleLoaded) under mock Linux sysfs states, non-Linux OSes, and permission errors.
  • Tests for dual-engine fallback in Manager ensuring failure of the kernel engine automatically boots the embedded engine.
  • Tests for KernelEngine and Engine interface compliance.
  • Complete Go test suite passes across all packages (go test ./...).

Docker Setup

To enable kernel module acceleration when running 3x-ui via Docker:

  1. Ensure the amneziawg module is loaded on the Linux host (sudo modprobe amneziawg).
  2. Add --cap-add=NET_ADMIN and mount the host modules directory:
docker run -d \
  --name 3x-ui \
  --restart unless-stopped \
  --network host \
  --cap-add=NET_ADMIN \
  -v /etc/x-ui:/etc/x-ui \
  -v /lib/modules:/lib/modules:ro \
  ghcr.io/mhsanaei/3x-ui:latest

If CAP_NET_ADMIN or the kernel module is omitted, the container runs normally using the embedded userspace engine.

Breaking changes

None. 100% backward-compatible.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6419 **Author:** [@rqzbeh](https://github.com/rqzbeh) **Created:** 9/5/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/amneziawg-kernel-module-engine` --- ### 📝 Commits (6) - [`2b62cbb`](https://github.com/MHSanaei/3x-ui/commit/2b62cbb62da420eaac060917350b907c9022534b) feat(amneziawg): add kernel module detection and probe helper - [`c927a45`](https://github.com/MHSanaei/3x-ui/commit/c927a4560711d9185e288de1a4328003d4ceff98) feat(amneziawg): add kernel module detection and preflight checks - [`b3af34d`](https://github.com/MHSanaei/3x-ui/commit/b3af34d560162ccd64758722fe176580f391e6b5) feat(amneziawg): abstract engine interface with automatic fallback in manager - [`575e61b`](https://github.com/MHSanaei/3x-ui/commit/575e61b37adbbfa99c6ad989a63c164adb5519af) feat(amneziawg): implement native kernel module engine with routing lifecycle - [`abea642`](https://github.com/MHSanaei/3x-ui/commit/abea642cdb3b7f94c722af98162b3474a21305ca) docs(amneziawg): document kernel module acceleration and docker setup - [`a9e1384`](https://github.com/MHSanaei/3x-ui/commit/a9e1384a9a8584e55e602e0f10094ded393cb1e5) fix(amneziawg): resolve golangci-lint linter issues in kernel routing ### 📊 Changes **11 files changed** (+700 additions, -17 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/en/config/amneziawg.mdx` (+42 -8) 📝 `internal/amneziawgnet/diagnostics.go` (+3 -6) ➕ `internal/amneziawgnet/engine.go` (+15 -0) ➕ `internal/amneziawgnet/engine_fallback_test.go` (+61 -0) ➕ `internal/amneziawgnet/kernel_detect.go` (+64 -0) ➕ `internal/amneziawgnet/kernel_detect_test.go` (+141 -0) ➕ `internal/amneziawgnet/kernel_engine.go` (+137 -0) ➕ `internal/amneziawgnet/kernel_engine_test.go` (+22 -0) ➕ `internal/amneziawgnet/kernel_routing_linux.go` (+154 -0) ➕ `internal/amneziawgnet/kernel_routing_other.go` (+29 -0) 📝 `internal/amneziawgnet/manager.go` (+32 -3) </details> ### 📄 Description ## Summary Adds native Linux kernel module acceleration (`amneziawg-linux-kernel-module`) for AmneziaWG inbounds with automatic, seamless fallback to the embedded `amneziawg-go` + `gVisor` userspace engine when unsupported or unprivileged. ## Why While the embedded `amneziawg-go` engine provides 100% portability without host kernel dependencies, high-bandwidth deployments benefit significantly from zero-copy native kernel processing. This dual-engine architecture probes for the kernel module at runtime: - If the Linux host has `amneziawg.ko` loaded and the process has `CAP_NET_ADMIN`, 3x-ui manages native `awg<id>` interfaces and routes peer traffic via policy routing / TPROXY into Xray, preserving full Xray routing rules, outbounds, sniffing, and live per-client accounting. - If the kernel module is absent, unprivileged (e.g. standard unprivileged Docker), or on non-Linux platforms, 3x-ui automatically falls back to the embedded userspace engine with zero configuration changes. ## Type of change - [x] New feature ## Areas affected - [x] Backend (API endpoints, login, settings) - [x] Xray config generation - [x] Documentation ## How was this tested? - Unit tests for preflight detection (`DetectKernelSupport` and `IsKernelModuleLoaded`) under mock Linux sysfs states, non-Linux OSes, and permission errors. - Tests for dual-engine fallback in `Manager` ensuring failure of the kernel engine automatically boots the embedded engine. - Tests for `KernelEngine` and `Engine` interface compliance. - Complete Go test suite passes across all packages (`go test ./...`). ## Docker Setup To enable kernel module acceleration when running 3x-ui via Docker: 1. Ensure the `amneziawg` module is loaded on the Linux host (`sudo modprobe amneziawg`). 2. Add `--cap-add=NET_ADMIN` and mount the host modules directory: ```bash docker run -d \ --name 3x-ui \ --restart unless-stopped \ --network host \ --cap-add=NET_ADMIN \ -v /etc/x-ui:/etc/x-ui \ -v /lib/modules:/lib/modules:ro \ ghcr.io/mhsanaei/3x-ui:latest ``` If `CAP_NET_ADMIN` or the kernel module is omitted, the container runs normally using the embedded userspace engine. ## Breaking changes None. 100% backward-compatible. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [x] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [x] I updated the Wiki / README / API docs if user-facing behavior changed. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:07:29 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6261
No description provided.