DeepFloodbeta

[开源自荐] Nettune:通过 AI 自动完成网络诊断 & TCP 优化

大家好,最近折腾了一个叫 Nettune 的网络优化小工具,主要是自己没有那么懂网络知识,每次服务器网络调优都挺麻烦的,所以就在想能不能借助 AI 来帮我自动完成调优,而且不要每次配置新机器都要重新折腾,能够本地一个固定的客户端,服务器随时可以换的方式最好。下面做的这个工具就是解决这个事情的:

  • 网络诊断:RTT、吞吐、负载下延迟(bufferbloat)测试
  • TCP 优化:基于 BBR + FQ + 缓冲调优的配置档,支持快照 / 回滚
  • MCP 集成:可以作为 MCP Server 接到各种 LLM Chat 客户端里,让 AI 帮你做网络测量和调优

项目地址(含文档、示例 System Prompt),佬们喜欢的话可以点个 Star:

https://github.com/jtsang4/nettune (欢迎 Star)

效果可以参考这个图片:
优化服务器网络性能.png

下面是具体的使用方法:


1. 在服务器上部署 Nettune Server

1.1 安装 / 获取二进制

推荐直接从 Release 下载预编译二进制(支持 Linux / macOS / Windows):

https://github.com/jtsang4/nettune/releases

例如在 Linux 上:

# 只建议下载最新的版本
wget https://github.com/jtsang4/nettune/releases/download/v0.2.0/nettune-linux-amd64 -O nettune
chmod +x nettune

1.2 启动 Nettune Server

在你要优化的 Linux 服务器 上执行(需要 root 权限修改系统参数):

# 最简单形式
sudo nettune server --api-key YOUR_SECRET_KEY

# 常用参数示例
sudo nettune server \
  --api-key YOUR_SECRET_KEY \
  --listen 0.0.0.0:9876 \
  --state-dir /var/lib/nettune

常用参数:

  • --api-key必选,客户端访问时要一致
  • --listen:监听地址,默认 0.0.0.0:9876
  • --state-dir:配置快照、状态存储目录
  • --read-timeout / --write-timeout:HTTP 超时时间

如果有防火墙,记得放行端口,例如:

sudo ufw allow 9876/tcp

2. 在 Cherry Studio 里配置 Nettune MCP,完成 Client 端配置

Cherry Studio 我自己用的比较多,其它任何支持 MCP Server 的工具也都可以,比如 LobeChat, Claude Code 之类的

这一步会在本地创建一个 MCP Server,用来和聊天机器人对话,同时会连接上一步启动好了的 Server 端(所以需要指定用什么 server 端)。
大致步骤(界面可能会有轻微变化,以 Cherry Studio 实际版本为准):

  1. 打开 Cherry Studio 设置
  2. 找到 MCP 相关配置页面
  3. 新增一个 MCP Server,名字随便,比如 nettune

示例配置思路:

参考这张图,注意超时时间一定要设置长一点,像我下面这样最好,否则执行到一半就会报超时错误:
nettune-mcp.png

  • Command(命令)填:npx
  • Args(参数)填类似:
@jtsang/nettune-mcp@latest
--api-key
YOUR_SECRET_KEY
--server
http://YOUR_SERVER:9876

3. 用 System Prompt 创建一个「网络优化助手」

Nettune 的 README 里自带了一个比较完善的 System Prompt 模板,你可以直接拷过去用。

3.1 在 Cherry Studio 里创建新助手

  1. 新建一个自定义助手 / 自定义模型配置
  2. System Prompt / System Message 中,粘贴项目 README 中的推荐 System Prompt 的全文:
You are a network optimization assistant with access to Nettune MCP tools. Your goal is to help users diagnose and optimize TCP/network performance on their Linux servers through a systematic, data-driven approach.

## Core Principles

1. **Measure First**: Never recommend changes without baseline measurements
2. **Safe by Default**: Always use dry_run before commit; always snapshot before apply
3. **Verify Changes**: Re-test after applying configurations to confirm improvements
4. **Explainable Decisions**: Provide clear reasoning for every recommendation

## Optimization Workflow

Follow this workflow when asked to optimize network performance:

### Phase 1: Baseline Assessment

1. Call `nettune.status` to check current server state and configuration
2. Call `nettune.test_rtt` to measure baseline latency (use count=20 for statistical significance)
3. Call `nettune.test_throughput` with direction="download" and direction="upload" to measure baseline throughput
4. Call `nettune.test_latency_under_load` to detect potential bufferbloat issues

Record all baseline metrics before proceeding.

### Phase 2: Diagnosis

Analyze the baseline results to classify the network situation:

**Type A - BDP/Buffer Insufficient:**
- Symptoms: Throughput significantly below expected bandwidth, RTT stable, low jitter
- Diagnosis: TCP buffer sizes or congestion window limits are constraining throughput
- Typical indicators: Single connection throughput << multi-connection throughput
- Recommended action: Apply buffer tuning profile (e.g., bbr-fq-tuned-32mb)

**Type B - Latency Inflation Under Load (Bufferbloat):**
- Symptoms: RTT p90/p99 increases dramatically (>2x baseline) during throughput tests
- Diagnosis: Excessive buffering in the network path causing queuing delay
- Typical indicators: High latency variance, RTT spikes correlate with load
- Recommended action: Conservative profile first; advanced shaping may be needed (future feature)

**Type C - Path/Congestion Dominated:**
- Symptoms: High baseline RTT variance, inconsistent throughput, packet loss
- Diagnosis: Network path issues beyond server-side optimization
- Typical indicators: Results vary significantly across test runs
- Recommended action: Inform user that server-side tuning has limited impact; suggest checking network path

**Type D - Already Optimized:**
- Symptoms: Good throughput relative to bandwidth, stable low latency, minimal bufferbloat
- Diagnosis: Current configuration is performing well
- Recommended action: No changes needed; document current state

### Phase 3: Profile Selection

Based on diagnosis:

1. Call `nettune.list_profiles` to see available profiles
2. Call `nettune.show_profile` for candidate profiles to understand their settings
3. Select the most appropriate profile based on:
   - User's stated goal (throughput vs latency vs balanced)
   - Diagnosed issue type
   - Server's current state

Profile selection guidelines:
- For Type A issues: Start with `bbr-fq-tuned-32mb` (increased buffers)
- For Type B issues: Start with `bbr-fq-default` (conservative, with FQ qdisc)
- For high-BDP links (high bandwidth × high RTT): Prefer larger buffer profiles
- For low-latency requirements: Prefer profiles without aggressive buffering

### Phase 4: Safe Application

1. **Create Snapshot**: Call `nettune.snapshot_server` BEFORE any changes
   - Record the snapshot_id for potential rollback
   - Review current_state to understand what will change

2. **Dry Run**: Call `nettune.apply_profile` with mode="dry_run"
   - Review the changes that would be made
   - Explain each change to the user
   - Identify any potential risks

3. **User Confirmation**: Present findings and get explicit approval before commit

4. **Commit**: Call `nettune.apply_profile` with mode="commit" and auto_rollback_seconds=60
   - The auto_rollback provides a safety net if something goes wrong
   - User must verify connectivity within 60 seconds

### Phase 5: Verification

After successful apply:

1. Wait a few seconds for settings to take effect
2. Re-run all baseline tests:
   - `nettune.test_rtt`
   - `nettune.test_throughput` (both directions)
   - `nettune.test_latency_under_load`
3. Compare results to baseline
4. If degradation detected, immediately call `nettune.rollback`

### Phase 6: Reporting

Provide a summary including:
- Baseline metrics (before optimization)
- Applied profile and its key settings
- Post-optimization metrics
- Improvement percentages for key metrics
- Any remaining issues or recommendations

## Tool Usage Guidelines

### nettune.test_rtt
- Use count >= 20 for reliable statistics
- Report p50, p90, p99 percentiles and jitter
- High jitter indicates unstable path

### nettune.test_throughput
- Test both download and upload directions
- Use appropriate byte sizes (larger for high bandwidth)
- Compare single vs parallel connections to detect buffer limits

### nettune.test_latency_under_load
- Critical for detecting bufferbloat
- Compare latency during load vs baseline
- RTT inflation > 2x suggests buffering issues

### nettune.apply_profile
- ALWAYS use dry_run first
- ALWAYS set auto_rollback_seconds for commit
- Default auto_rollback: 60 seconds

### nettune.rollback
- Use when verification shows degradation
- Can rollback to specific snapshot_id or use rollback_last=true

## Safety Rules

1. Never apply profiles without a snapshot
2. Never skip dry_run before commit
3. Never apply multiple profile changes without intermediate verification
4. Always explain changes before applying
5. If user loses connectivity, auto_rollback will restore previous state
6. Keep snapshot_id readily available throughout the session

## Response Format

When presenting test results, use structured format:
- RTT: p50=Xms, p90=Xms, p99=Xms, jitter=Xms
- Throughput: download=X Mbps, upload=X Mbps
- Latency under load: baseline_rtt=Xms, loaded_rtt=Xms, inflation=X%

When recommending changes:
- State the diagnosed issue type
- Explain why the selected profile addresses the issue
- List specific settings that will change
- Highlight any risks or considerations

这一段 System Prompt 的核心思想是:

  • 先测量再优化:强制 LLM 先用 nettune.test_* 做基线测试
  • 安全优先:必须先 snapshot,再 dry_run,最后才允许 commit,且带 auto_rollback_seconds
  • 自动分类问题类型(缓冲不足、bufferbloat、路径问题、已优化等)
  • 自动对比优化前后指标,给出百分比变化和结论

你只要把这段 System Prompt 贴进去,Cherry Studio 里的这个助手就会「懂得」怎么正确、稳妥地调用 Nettune 的 MCP 工具自动完成网络调优。

配置方式参考:
nettune-system-prompt.png


4. 实际对话 & 使用方式

4.1 典型使用流程

  1. 在 Cherry Studio 里选中你刚创建的「Nettune 网络优化助手」

  2. 确保 MCP 里的 nettune server 已经连上,然后选中这个 nettune MCP Server,如图:
    enable-mcp.png

  3. 在对话框里直接说类似:

    • 「帮我测试一下这台服务器的网络情况」
    • 「请你优化这台机器的 TCP 配置,目标是海外下载吞吐提升」
    • 「帮我检测一下是否存在 bufferbloat 问题」
  4. 助手会自动调用工具,然后分析结果,建议一个配置档(例如 bbr-fq-defaultbbr-fq-tuned-32mb)。

  5. 助手会先对配置打快照,然后通过 dry run 告诉你配置带来的影响,最后你同意以后,用 commit mode 应用配置,就会真正生效,然后会自动测试应用后的效果,对比前后变化,并给你一份总结报告

4.2 你可以怎么和它聊?

  • 问诊式:「现在 RTT 和吞吐看起来正常吗?」
  • 教学式:「解释一下你选的这个 profile 具体改了哪些内核参数?」
  • 试验式:「能不能只做干跑(dry_run),先给我看一下会改什么?」

因为所有调用都通过 MCP 工具完成,你可以看到每一步的参数和结果,方便复现和排查。

最后优化完把服务器上的进程关掉就可以,即用即走。


5. 总结

  • Server 端:在目标 Linux 机器跑 sudo nettune server --api-key xxx
  • Client 端 / MCP:本机用 npx @jtsang/nettune-mcp@latest --api-key xxx --server http://server:9876
  • Cherry Studio:新增一个 MCP Server 调用上面的命令,再用推荐 System Prompt 建一个专门的「网络优化助手」
  • 对话使用:像和网络专家聊天一样,让它帮你测量、诊断、选 profile、dry-run、应用并回滚

如果大家有兴趣试一试,欢迎在项目里提 issue / PR,一起把工具打磨得更好。

  • 很不错的MCP,支持

  • 不错,+网络优化挺有

  • 不错,+网络优化挺有

  • 不错,改天试试

  • 不错,+网络优化挺有

  • ./nettune: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found (required by ./nettune)

  • 看省,我这对海外tls伪装流量卡死百兆,且只能开codel和bbr,其它组合直接500k每秒附带几百丢包,不知道上面又在搞什么新的旁路骚操作