0 写在前面
因科研需要用到p4,于是在网上寻找安装教程,但发现网上教程大多只是贴了运行的命令或者一个脚本,或许那些博客的作者配置比较顺利。不过对刚开始接触p4并要配置环境的新手来说只看脚本命令可能会遇到很多问题,这篇教程是本人按照网上的教程配置的,记录了过程中遇到的问题,每个命令我都截了运行的结果图,方便大家在配置的过程中参考我的输出结果,p4的环境虽然只分为安装protobuf、p4c、bmv2这三个工具,但我走了不少弯路,遇到了各种问题,足足配置了5天。实验室的同学配置过一次也花了两个星期,但是当时没有写教程,而p4的环境由于依赖的库很多,所以每一步都可能出现错误。
因此当bmv2配置结束的时候,我回想配置的过程说简单也简单,毕竟不超过20条命令,但是说难也难,配置了5天,遇到一大堆问题。这边教程并不是最简洁的,有的命令重复执行(确认依赖已经安装),有的图贴的过于繁琐,但是最主要还是为了力求真实的记录配置过程,写这篇教程的初衷是为了让像我一样的初学者在配置过程中能少走一些弯路,希望这篇教程能帮助到后来者。
配置的过程中会遇到许多的问题,这里我只记录了我遇到的问题,可能大家在配置的时候会遇到新的问题,因此,既然决定要配置,就一定要耐心,静下心来操作。
这里,推荐几个我参考了的博客:
(1)源码下载,p4所有的源码和教程都来源于这个github:https://github.com/p4lang 这个是官方的教程,推荐大家多看看这个,因为第三方教程的环境和依赖都不同,都是针对自己的机器安装的,所以最好的还是github的官方readme文档。
(2)安装教程有以下几个:
主要参考:https://www.sdnlab.com/19912.html
辅助参考:
https://www.cnblogs.com/qq952693358/p/7463204.html
https://steeven.iteye.com/blog/2330830
https://www.cnblogs.com/qq952693358/p/5905536.html
https://blog.csdn.net/zhangmeimei_pku/article/details/79479055
https://www.sdnlab.com/22466.html
https://www.sdnlab.com/22512.html
https://www.cnblogs.com/ljy1227476113/p/10524035.html
http://sunyongfeng.com/201705/networks/p4/repo_p4app.html
本次配置p4所用到的安装文件我已经打包并上传到CSDN的p4-source.tar.gz中,文件内容如下,如果没有积分的朋友可以发邮件给我:[email protected]
1 准备工作
1.1 查看系统环境
本文使用Ubuntu 16.04 LTS,内核版本Linux ubuntu 4.15.0-46-generic,应该影响不大,系统信息如下图:

1.2 安装部分依赖
1.2.1 安装常用工具
安装一些常用的工具,用于后面的使用,命令如下:
$sudo apt-get install g++ git automake libtool libgc-dev bison flex libfl-dev libgmp-dev libboost-dev libboost-iostreams-dev pkg-config python python-scapy python-ipaddr curl wget tcpdump cmake

1.2.2 安装protobuf依赖库
protocbuf的官网所列出需要如下依赖:autoconf, automake, libtool, curl, make, g++, unzip, gmock等,分两部分安装,先按照autoconf, automake, libtool, curl, make, g++, unzip,执行如下命令:
$sudo apt-get install autoconf automake libtool curl make g++ unzip

检查安装是否完成,输入以下命令:
$sudo apt-get install autoconf automake g++ curl libtool make unzip
出现如下图的输出,即***已经是最新版本,则表示安装完成。

1.2.3 安装gmock
(1)下载googlemock源码
$git clone git://github.com/paulsapps/gmock-1.7.0


(2)进入gmock-1.7.0/make目录,然后编译
$cd gmock-1.7.0
$cd make
$make

(3)gmock单元检测
$./gmock_test

gmock单元测试务必保证全部通过,未通过先不要进行下一步,否则之后可能会出现意想不到的错误。
(4)重命名
最后一步,将gmock-1.7.0重命名为gmock,暂时先放着,留在安装protobuf的时候用到。

2 安装protobuf
2.1 下载protubuf源码包
官网网址:https://github.com/protocolbuffers/protobuf/releases?after=v3.2.1
官网有很多个版本,我写博客的时候最新版本已到了3.7.1了,这里个人建议下载3.2.0版本的,网上其他博客的教程都是默认获取最新版本的链接,我试过,最新版的3.7.1,结果make check死活通不过(可能是本人机器环境的原因),比较建议下载老版本的,这样会稳定一点,遇到的问题会更少。我这里下载的是3.7.1版本的。
$wget https://github.com/protocolbuffers/protobuf/archive/v3.2.0.zip
下载最新版的命令如下:
$git clone https://github.com/google/protobuf.git

得到protobuf-3.2.0的源码压缩包,如下图所示:

2.2 创建目录p4,并把v3.2.0.zip移动到p4目录中
$mkdir p4
$mv v3.2.0.zip p4/


2.3 解压protobuf
进入p4目录,并解压v3.2.0.zip,命令如下:
$cd p4
$unzip v3.2.0.zip


2.4 移动gmock
进入protobuf,并之前重命名的gmock文件夹移动protobuf中。

2.5 再次确认gmock是否可用
进入p4/protobuf/gmock/make目录,进行单元测试。
$./gmock_test

make check的输出结果非常重要,必须要进行!!!需要所有的模块都PASS通过才能进行下一步,否则之后很可能会出现意想不到的错误。如果无法全部pass通过,请先不要进行下一步,往回想办法解决问题。
至此,protobuf的准备工作完成,继续往下操作。
2.6 进入protobuf目录,查看文件
进入protobuf目录:
$cd p4
$cd protobuf
$dir

2.7 继续操作
执行如下命令:
$./autogen.sh

2.8 继续操作
执行如下命令:
$./configure


2.9 编译
make这一步,时间比较长,需要耐心等待
$make


2.10 单元检测
进行单元测试,检测是否编译正确,确认protobuf能正常工作。
$make check


make check的输出结果非常重要,必须要进行!!!需要所有的模块都PASS通过才能进行下一步,否则之后很可能会出现意想不到的错误。如果无法全部pass通过,请先不要进行下一步,往回想办法解决问题。
2.11 安装
安装protobuf,执行如下命令:
$sudo make install


2.12 刷新共享库缓存
执行如下命令:
$sudo ldconfig

2.13 查看版本
$protoc --version

至此,protubuf安装完成。
至此,protubuf安装完成。
至此,protubuf安装完成。
3 安装p4c
3.1 官方源码及文档
官方源码及教程:https://github.com/p4lang/p4c
p4c的安装官方要求安装的依赖: https://github.com/p4lang/p4c#ubuntu-dependencies
Dependencies
Ubuntu 16.04 is the officially supported platform for p4c. There's also unofficial support for macOS 10.12. Other platforms are untested; you can try to use them, but YMMV.
--A C++11 compiler. GCC 4.9 or later or Clang 3.3 or later is required.
--git for version control
--GNU autotools for the build process
--CMake 3.0.2 or higher
--Boehm-Weiser garbage-collector C++ library
--GNU Bison and Flex for the parser and lexical analyzer generators.
--Google Protocol Buffers 3.0 or higher for control plane API generation
--GNU multiple precision library GMP
--C++ boost library (minimally used)
--Python 2.7 for scripting and running tests
--Optional: Documentation generation (enabled when configuring with --enable-doxygen-doc) requires Doxygen (1.8.10 or higher) and Graphviz (2.38.0 or higher).
Backends may have additional dependencies. The dependencies for the backends included with p4c are documented here:
--BMv2
--eBPF
--graphs
3.2 安装依赖
必须安装如下依赖,否则无法完整安装p4c。
$sudo apt-get install cmake g++ git automake libtool libgc-dev bison flex libfl-dev libgmp-dev libboost-dev libboost-iostreams-dev libboost-graph-dev llvm pkg-config python python-scapy python-ipaddr python-ply tcpdump

3.3 下载p4c源码
$git clone --recursive https://github.com/p4lang/p4c.git

备注:如果出现“fatal:过早的文件结束符(EOF) fatal:index-pack failed”的报错,则需要把https改为git,使用如下命令:
$git clone --recursive git://github.com/p4lang/p4c.git

3.4 将p4c移动到p4目录中

3.5 进入p4c文件夹,执行命令
$cd p4c
$./bootstrap.sh
./bootstrap.sh 可以替换为 mkdir build


命令结束执行结束后,会发现p4c下面产生build文件夹。

3.6 进入p4c的build目录,并cmake编译
$cd build

备注:如果cmake ..报如下错误,是因为build创建目录顺序错误,请先删除build目录,然后重新创建build目录,解决过程如下图所示:

3.7 make编译
$make -j4


3.8 p4c单元测试
$make check -j4


备注:各版本p4c的测试数目不完全一样,这个不影响结果,只要通过100%就可以,但是必须保证全部通过,如果出现failed或error请不要进行下一步,否则后面可能会出现意想不到的错误。
3.9 安装
$sudo make install


3.10 查看版本
$p4c --version

至此,p4c安装完成。
至此,p4c安装完成。
至此,p4c安装完成。
4 安装bmv2
4.1 下载bmv2
behavioral-model又叫bmv2交换机模块,以下检称bmv2。
$git clone git://github.com/p4lang/behavioral-model.git


4.2 进入目录,并执行命令
$./install_deps.sh

可以看到出现报错,因为无法连接到下载thrift源码包的网址,http://archive.apache.org/dist/thrift/0.9.2/thrift-0.9.2.tar.gz,这里采用手动下载的方式。下载好源码包后解压,并放到p4/behavioral-model目录下,如下图所示:

然后打开/home/yongpu/p4/behavioral-model/travis/install-thrift.sh文件,进行编辑

修改前内容如下:

注释掉
#wget http://archive.apache.org/dist/thrift/0.9.2/thrift-0.9.2.tar.gz
#tar -xzvf thrift-0.9.2.tar.gz
#cd thrift-0.9.2
然后插入如下一行代码,也就是thrift-0.9.2的绝对路径,也就是behavioral-model下的thrift-0.9.2目录的位置,修改后保存文件。
$cd /home/yongpu/p4/behavioral-model/thrift-0.9.2
修改后如下图所示:
然后重新运行命令
$./install_deps.sh


这里出现如下报错:
HEAD 目前位于 c7e718a... Merge pull request #7 from bnewbold/patch-1
Traceback (most recent call last):
File "/usr/bin/pip", line 9, in <module>
from pip import main
ImportError: cannot import name main
报错的原因是我用的是pip10.0.1,这里好像是版本的问题,可以使用如下命令查看pip版本
$pip --version

解决方法如下:
参考这个博客的解决办法:https://blog.csdn.net/cow66/article/details/80069309
修改 /usr/bin/pip 文件,使用sudo vim /usr/bin/pip修改。
将原本如下内容
from pip import main
if __name__ == '__main__':
sys.exit(main())
修改为:
from pip import __main__
if __name__ == '__main__':
sys.exit(__main__._main())
原内容:

修改后内容如下,然后使用 :wq! 保存退出。

使用cat查看,查看是否修改成功:

然后升级pip,最后查看版本,命令如下,可以看到pip升级到19.0.3版本。
$sudo apt-get install python-pip
$sudo pip install --upgrade pip
$pip --version

再次执行
$./install_deps.sh


小插曲:期间报了一个错,截图如下,这是因为磁盘空间不足,清理一下回收站或者扩充下空间即可,查看磁盘空间命令为:
df -hl

4.3 执行命令
$./autogen.sh

4.4 执行命令
$./configure


4.5 编译
$make


4.6 安装
$sudo make install -j4


4.7 单元测试
bmv2的单元测试一定要在4.6安装完成之后再进行,否则无法完全检测通过。
$make check -j4




make check的输出结果非常重要,必须要进行!!!需要所有的模块都PASS通过才能进行下一步,否则之后很可能会出现意想不到的错误。如果无法全部pass通过,请先不要进行下一步,往回想办法解决问题。
4.8 查看bmv2版本
$cat VERSION
至此,bmv2的安装完成!
至此,bmv2的安装完成!
至此,bmv2的安装完成!
5 总结
总的来说,配置p4的安装环境一共需要安装3个东西,分别是protobuf、p4c、bmv2,建议大家多看看github的教程,然后遇到问题先去看看教程是否自己遗漏了什么,最常见的问题是缺少了依赖,因为p4的配置需要的依赖很多,所以make check不通过的原因大多数是某个依赖没有安装。
至此,p4环境配置完成!
至此,p4环境配置完成!
至此,p4环境配置完成!