# p4-env **Repository Path**: kong-hao_1/p4-env ## Basic Information - **Project Name**: p4-env - **Description**: 在Ubuntu上快速搭建P4实验环境 - **Primary Language**: Shell - **License**: MIT - **Default Branch**: advanced - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 9 - **Created**: 2024-10-04 - **Last Updated**: 2024-10-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 为了那些不必要的痛苦(在Ubuntu中搭建基于P4的SDN实验环境) 本教程主要针对 **Ubuntu20.04** 系统,全面将实验环境升级至`python3`版本,提供`shell`脚本自动下载、编译及安装所有实验模块。 ![demo](./demo.png) **!不推荐** 如果需要使用`Ubuntu18.04`或`python2`,请参考[主分支](https://gitee.com/poohdang/p4-env/tree/master/)。 **请不要**直接运行脚本而不浏览其内容! 安装过程中出现问题,尝试查看[FAQ](#faq)获取帮助。 --- * [实验环境](#实验环境) * [实验模块介绍](#实验模块介绍) * [环境搭建](#环境搭建) * [开始之前](#开始之前) * [下载源代码](#下载源代码) * [编译并安装](#编译并安装) * [快速开始第一个P4实验](#快速开始第一个p4实验) * [FAQ](#faq) ## 实验环境 * 操作系统:Ubuntu20.04 LTS * `python 3.x` and `pip` > 确保`python3`指令指向期望的解释器(我在使用系统自带的python3.8) ```sh $ sudo apt install python3 python3-pip $ python3 -V $ python3 -m pip -V ``` * Recommended minimum system requirements: 4 GB of RAM 1 GB of swap 2 cores 8GB free hard drive space (~4 GB to build everything) ## 实验模块介绍 | Github项目 | 功能 | 版本 | 来自于 | | --- | --- | --- | --- | | [protobuf](https://github.com/protocolbuffers/protobuf) | 数据消息交换格式,grpc的默认选项 | v3.18.1 | Google | | [grpc](https://github.com/grpc/grpc) | 远程程序调用(RPC)框架,PI基于此实现 | v1.43.2 | Google | | [PI](https://github.com/p4lang/PI) | P4 Runtime Server | v0.1.0 | P4lang | | [bmv2](https://github.com/p4lang/behavioral-model) | 支持P4编程的软件交换机 | 1.15.0 | P4lang | | [p4c](https://github.com/p4lang/p4c) | P4 编译器 | v1.2.2.1 |P4lang | | [mininet](https://github.com/mininet/mininet) | 网络仿真器 | Latest | Mininet | | [tutorials](https://github.com/p4lang/tutorials) | P4官方提供的快速上手教程 | Latest | P4lang | 安装bmv2和PI之前还需要以下模块作为依赖`bmv2_deps` | Github项目 | 版本 | 来自于 | | --- | --- | --- | | nnpy | - | nanomsg | | [nanomsg](https://github.com/nanomsg/nanomsg/releases/tag/1.) | 1.0.0 | nanomsg | | [thrift](https://github.com/apache/thrift/releases/tag/0.11.0) | 0.11.0 | apache | ## 环境搭建 #### 开始之前 1. 安装Git ``` $ sudo apt install git ``` 2. 设置工作目录并克隆本仓库 ```shell # 以下工作目录可自定义,此处为示例 $ mkdir -p ~/Workspace/P4 && echo "export P4_HOME=~/Workspace/P4" >> ~/.bashrc $ source ~/.bashrc $ cd $P4_HOME # 该环境变量必须生效,后续脚本需要 $ git clone https://gitee.com/poohdang/p4-env.git $ cd p4-env ``` 3. 安装必要的依赖和编译工具 ``` $ sudo chmod 755 p4*.sh $ ./p4-deps.sh ``` #### 下载源代码 ###### 方法一:从Github上直接下载 (部分模块国内用户可能下载速度过慢) ``` $ ./p4-git.sh ``` ###### 方法二:对部分URL进行替换 (使用`https://hub.fastgit.org`加速的仓库) 似乎已经失效 ``` $ ./p4-git-china.sh ``` ###### 方法三: 链接:https://pan.baidu.com/s/1jDwfEc1wkDQARgGorho5kA?pwd=9bpt 提取码:9bpt #### 编译并安装 ```shell $ ./p4-install.sh # 脚本支持选择性编译单个模块(以PI为例) $ ./p4-install.sh pi #(mininet/protobuf/grpc/bmv2_deps/pi/bmv2/p4c) ``` #### 检查python搜索路径 > 确保两个重要的`site-packages`路径已加入当前解释器的PYTHONPATH(见下图) ![py_path](./py_path.png) ``` $ echo "/home/$USER/.local/lib/python3.8/site-packages" >> p4-env.pth $ sudo cp -f p4-env.pth /usr/local/lib/python3.8/dist-packages/ ``` ## 快速开始第一个P4实验 ``` $ cd tutorials/exercises/basic $ make mininet> ... ``` 若Python报出 `ModuleNotFoundError: No module named 'p4.config'` 的错误,执行以下指令。[issue](https://gitee.com/poohdang/p4-env/issues/I61UZY#note_14535545_link) ``` sudo cp -rf $P4_HOME/p4-env/p4 /usr/local/lib/python3.8/site-packages/ ``` 若Python报出 `ModuleNotFoundError: No module named 'google.protobuf'` 的错误,执行以下指令 ``` $ cd /usr/local/lib/python3.8 $ sudo cp -rf dist-packages/protobuf-3.18.1-py3.8-linux-x86_64.egg/google/protobuf site-packages/google/ ``` #### 退出 ``` mininet> exit $ sudo make stop $ sudo make clean ``` 查看`basic/README.md`文件获取`basic`实验的详细说明 ## FAQ #### 一、为什么分为两个脚本? 将下载过程与编译/安装过程分离,同时针对下载过程进行优化。 #### 二、为什么特别指定`protobuf`和`grpc`的版本? 满足`PI`对两者的版本要求。可进入[PI的主页](https://github.com/p4lang/PI)进行确认,并按需对脚本进行更改。 #### 三、为什么在安装`Mininet` 时使用`-nvw`选项? -n: install Mini(N)et dependencies + core files -v: install Open (V)switch -w: install OpenFlow (W)ireshark dissector 通过`/util/install.sh -h`查看更多选项 ## 问题反馈 评论区,或 ndsczx@foxmail.com ## LICENSE [MIT](./LICENSE)