P4Sentinel 是一个基于 P4 可编程数据平面的微服务流量治理系统,将流控与熔断逻辑迁移至交换机层执行,实现微秒级响应的分布式限流能力。
本项目复刻并简化了 Alibaba Sentinel 的核心流控与熔断机制,适用于服务集群前置可编程交换机的场景。
graph TB
subgraph 控制平面
C[Go 控制器<br/>P4Runtime gRPC]
CFG[rules.yaml<br/>流控/熔断规则]
end
subgraph 数据平面
SW[BMv2 simple_switch_grpc<br/>P4 程序]
end
subgraph 微服务集群
H1[Host 1<br/>10.0.0.1]
H2[Host 2<br/>10.0.0.2]
end
CFG --> C
C -- "SetPipeline / WriteTableEntry" --> SW
SW -- "Digest 上报统计" --> C
H1 <--> SW
H2 <--> SW
- 流量控制:支持 Direct(固定阈值)和 WarmUp(预热冷启动)两种限流策略
- 熔断降级:支持三状态(Closed / Open / Half-Open)熔断器,支持 Direct 和 WarmUp 恢复策略
- 微秒级精度:统计窗口和判断逻辑均在数据平面完成
- Digest 上报:流控统计数据通过 P4Runtime Digest 机制实时上报控制器
P4Sentinel/
├── README.md
├── LICENSE # Apache-2.0
├── Makefile
├── .gitignore
├── p4src/ # P4_16 源码
│ ├── sentinel.p4 # 主入口
│ ├── headers.p4 # 头部定义
│ ├── parser.p4 # 解析器
│ ├── ingress.p4 # Ingress 逻辑(流控 + 熔断)
│ ├── egress.p4 # Egress 逻辑
│ ├── checksum.p4 # 校验和
│ └── deparser.p4 # 反解析器
├── build/ # p4c 编译产物(不入库)
├── topology/ # Mininet 拓扑脚本
│ ├── topo.py # 拓扑启动入口
│ ├── p4_switch.py # BMv2 交换机节点封装
│ └── p4_host.py # P4 实验用主机节点
├── controller/ # Go 控制器
│ ├── main.go # 入口(run / push-config / dump-counters)
│ ├── go.mod / go.sum
│ ├── internal/
│ │ └── rules/ # YAML 规则解析
│ └── configs/
│ └── rules.yaml # 示例规则配置
├── tests/
│ └── e2e.sh # 端到端测试脚本
└── docs/
└── architecture.md # 架构文档
| 依赖 | 版本要求 | 说明 |
|---|---|---|
| Ubuntu | 20.04 / 22.04 | 推荐 |
| p4c | >= 1.2 | P4 编译器 |
| behavioral-model | >= 1.15 | BMv2,需 simple_switch_grpc |
| Mininet | >= 2.3 | 网络仿真 |
| Go | >= 1.22 | 控制器编译 |
| Python | >= 3.8 | 拓扑脚本 |
# P4 工具链(参考 p4lang 官方文档)
# https://github.com/p4lang/p4c
# https://github.com/p4lang/behavioral-model
# Mininet
sudo apt-get install mininet
# Go (https://go.dev/dl/)
wget https://go.dev/dl/go1.22.5.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.22.5.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/binmake p4make controller# 需要 root 权限
make topo在另一个终端中:
cd controller
./controller run
# 或指定配置文件
./controller run configs/rules.yaml在 Mininet CLI 中:
mininet> h1 ping h2
mininet> iperf h1 h2# 启动控制器并持续监听 digest
./controller run [config.yaml]
# 仅下发 pipeline 和流表规则
./controller push-config [config.yaml]
# 读取并打印 register 值
./controller dump-counters [config.yaml]参见 controller/configs/rules.yaml 中的示例配置。
| 字段 | 说明 |
|---|---|
strategy |
direct(固定阈值)或 warm_up(预热) |
behavior |
reject(超阈值丢包) |
threshold |
限流阈值(PPS) |
stat_interval_us |
统计窗口(微秒) |
warm_up_period_us |
预热时长(微秒,仅 WarmUp 模式) |
warm_up_cold_factor |
冷启动因子(初始阈值 = threshold >> factor) |
| 字段 | 说明 |
|---|---|
retry_timeout_us |
熔断持续时间(微秒) |
threshold |
Half-Open 试探流量阈值(PPS) |
recovery_strategy |
恢复策略:direct 或 warm_up |
recovery_period_us |
恢复时长(微秒) |
warm_up_rate |
预热恢复速率(微秒/PPS) |
需要从源码编译 behavioral-model 并开启 gRPC 支持:
git clone https://github.com/p4lang/behavioral-model.git
cd behavioral-model
./install_deps.sh
./autogen.sh
./configure --with-pi --with-thrift
make -j$(nproc)
sudo make installunused 常量的 warning 可以忽略,这些常量保留给后续扩展使用。确保没有 error 即可。
检查:
simple_switch_grpc是否正在运行并监听正确端口rules.yaml中的addr是否匹配- 防火墙是否放行了 gRPC 端口