P4实战在Mininet里用simple_switch_grpc跑通你的第一个三层转发Demo当你第一次看到P4代码在Mininet环境中实现真实的三层转发时那种成就感就像看着自己设计的乐高模型突然动了起来。本文将带你完整走通这个流程——从P4源码编译、拓扑脚本编写到流表下发和功能验证。这不是概念讲解而是能立即上手的实操指南。1. 环境准备与工具链配置在开始之前确保你的系统已经安装以下组件BMv2P4软件交换机参考实现P4编译器p4cMininet网络模拟器P4Runtime相关依赖推荐使用Ubuntu 20.04 LTS作为基础环境通过以下命令安装核心组件# 安装依赖库 sudo apt install git cmake make g python3-pip \ libboost-dev libboost-system-dev libboost-thread-dev # 安装PI和BMv2 git clone https://github.com/p4lang/behavioral-model.git cd behavioral-model ./install_deps.sh ./autogen.sh ./configure make sudo make install # 安装P4编译器 git clone https://github.com/p4lang/p4c cd p4c mkdir build cd build cmake .. make -j4 sudo make install注意如果遇到protobuf版本冲突建议使用virtualenv创建隔离的Python环境2. P4程序设计与编译我们以一个基础的三层转发程序为例demo.p4关键部分包括header ipv4_t { bit32 dstAddr; bit32 srcAddr; // 其他标准IP头字段... } action ipv4_forward(bit9 egress_port) { standard_metadata.egress_spec egress_port; } table ipv4_lpm { key { hdr.ipv4.dstAddr: lpm; } actions { ipv4_forward; drop; } size 1024; default_action drop; }编译P4程序到BMv2可执行格式p4c --target bmv2 --arch v1model \ --p4runtime-files demo.p4info.txt \ -o build demo.p4编译完成后会生成三个关键文件demo.jsonBMv2的JSON配置demo.p4info.txtP4Runtime接口描述其他中间文件3. Mininet拓扑集成创建自定义拓扑脚本run_exercise.py关键组件包括3.1 拓扑结构定义class SingleSwitchTopo(Topo): def __init__(self, sw_path, json_path, thrift_port, pcap_dump, n, **opts): Topo.__init__(self, **opts) switch self.addSwitch(s1, sw_pathsw_path, json_pathjson_path, thrift_portthrift_port, pcap_dumppcap_dump) for h in range(n): host self.addHost(h%d % (h 1), ip10.0.0.%d/24 % (h 1), mac00:04:00:00:00:%02x % h) self.addLink(host, switch)3.2 网络启动配置def main(): topo SingleSwitchTopo(args.behavioral_exe, args.json, args.thrift_port, args.pcap_dump, num_hosts) net Mininet(topotopo, hostP4Host, switchP4RuntimeSwitch, controllerNone) net.start() # 配置主机路由和ARP表项 for n in range(num_hosts): h net.get(h%d % (n 1)) h.setARP(sw_addr[n], sw_mac[n]) h.setDefaultRoute(dev eth0 via %s % sw_addr[n])4. 流表下发与功能验证4.1 通过P4Runtime下发流表创建commands.txt文件定义转发规则table_add ipv4_lpm ipv4_forward 10.0.0.1/32 1 table_add ipv4_lpm ipv4_forward 10.0.0.2/32 2使用runtime_CLI工具加载规则./runtime_CLI.py commands.txt4.2 测试数据平面功能在Mininet CLI中启动测试终端mininet xterm h1 h2在h2上启动接收器./receive.py在h1上发送测试报文./send.py 10.0.0.2 Hello P4 World成功时会在h2终端看到接收到的消息证明三层转发正常工作。5. 常见问题排查指南当流程不工作时按以下步骤检查交换机日志检查tail -f /tmp/bm-0-log.ipc端口状态验证ps aux | grep simple_switch netstat -tulnp | grep 50051流表确认# 在runtime_CLI中 RuntimeCmd: table_dump ipv4_lpm抓包分析tcpdump -i s1-eth1 -nn -vv6. 进阶扩展方向基础demo跑通后可以尝试以下增强添加ACL功能在P4中实现简单的防火墙规则支持ICMP协议完善ping请求的响应处理集成sFlow监控添加流量采样功能多交换机拓扑构建更复杂的网络场景修改后的P4程序需要重新编译并更新到交换机p4c --target bmv2 --arch v1model -o build new_demo.p4 sudo mn -c ./run_exercise.py --json build/new_demo.json
P4实战:在Mininet里用simple_switch_grpc跑通你的第一个三层转发Demo
P4实战在Mininet里用simple_switch_grpc跑通你的第一个三层转发Demo当你第一次看到P4代码在Mininet环境中实现真实的三层转发时那种成就感就像看着自己设计的乐高模型突然动了起来。本文将带你完整走通这个流程——从P4源码编译、拓扑脚本编写到流表下发和功能验证。这不是概念讲解而是能立即上手的实操指南。1. 环境准备与工具链配置在开始之前确保你的系统已经安装以下组件BMv2P4软件交换机参考实现P4编译器p4cMininet网络模拟器P4Runtime相关依赖推荐使用Ubuntu 20.04 LTS作为基础环境通过以下命令安装核心组件# 安装依赖库 sudo apt install git cmake make g python3-pip \ libboost-dev libboost-system-dev libboost-thread-dev # 安装PI和BMv2 git clone https://github.com/p4lang/behavioral-model.git cd behavioral-model ./install_deps.sh ./autogen.sh ./configure make sudo make install # 安装P4编译器 git clone https://github.com/p4lang/p4c cd p4c mkdir build cd build cmake .. make -j4 sudo make install注意如果遇到protobuf版本冲突建议使用virtualenv创建隔离的Python环境2. P4程序设计与编译我们以一个基础的三层转发程序为例demo.p4关键部分包括header ipv4_t { bit32 dstAddr; bit32 srcAddr; // 其他标准IP头字段... } action ipv4_forward(bit9 egress_port) { standard_metadata.egress_spec egress_port; } table ipv4_lpm { key { hdr.ipv4.dstAddr: lpm; } actions { ipv4_forward; drop; } size 1024; default_action drop; }编译P4程序到BMv2可执行格式p4c --target bmv2 --arch v1model \ --p4runtime-files demo.p4info.txt \ -o build demo.p4编译完成后会生成三个关键文件demo.jsonBMv2的JSON配置demo.p4info.txtP4Runtime接口描述其他中间文件3. Mininet拓扑集成创建自定义拓扑脚本run_exercise.py关键组件包括3.1 拓扑结构定义class SingleSwitchTopo(Topo): def __init__(self, sw_path, json_path, thrift_port, pcap_dump, n, **opts): Topo.__init__(self, **opts) switch self.addSwitch(s1, sw_pathsw_path, json_pathjson_path, thrift_portthrift_port, pcap_dumppcap_dump) for h in range(n): host self.addHost(h%d % (h 1), ip10.0.0.%d/24 % (h 1), mac00:04:00:00:00:%02x % h) self.addLink(host, switch)3.2 网络启动配置def main(): topo SingleSwitchTopo(args.behavioral_exe, args.json, args.thrift_port, args.pcap_dump, num_hosts) net Mininet(topotopo, hostP4Host, switchP4RuntimeSwitch, controllerNone) net.start() # 配置主机路由和ARP表项 for n in range(num_hosts): h net.get(h%d % (n 1)) h.setARP(sw_addr[n], sw_mac[n]) h.setDefaultRoute(dev eth0 via %s % sw_addr[n])4. 流表下发与功能验证4.1 通过P4Runtime下发流表创建commands.txt文件定义转发规则table_add ipv4_lpm ipv4_forward 10.0.0.1/32 1 table_add ipv4_lpm ipv4_forward 10.0.0.2/32 2使用runtime_CLI工具加载规则./runtime_CLI.py commands.txt4.2 测试数据平面功能在Mininet CLI中启动测试终端mininet xterm h1 h2在h2上启动接收器./receive.py在h1上发送测试报文./send.py 10.0.0.2 Hello P4 World成功时会在h2终端看到接收到的消息证明三层转发正常工作。5. 常见问题排查指南当流程不工作时按以下步骤检查交换机日志检查tail -f /tmp/bm-0-log.ipc端口状态验证ps aux | grep simple_switch netstat -tulnp | grep 50051流表确认# 在runtime_CLI中 RuntimeCmd: table_dump ipv4_lpm抓包分析tcpdump -i s1-eth1 -nn -vv6. 进阶扩展方向基础demo跑通后可以尝试以下增强添加ACL功能在P4中实现简单的防火墙规则支持ICMP协议完善ping请求的响应处理集成sFlow监控添加流量采样功能多交换机拓扑构建更复杂的网络场景修改后的P4程序需要重新编译并更新到交换机p4c --target bmv2 --arch v1model -o build new_demo.p4 sudo mn -c ./run_exercise.py --json build/new_demo.json