P4环境搭建
环境要求:
本文安装使用的操作系统为ubuntu16.04,内存5G。
note:内存不足会出现“无法分配内存”的问题。
主要参考文献
1.完整脚本参考
官方的参考:https://github.com/p4lang/tutorials/blob/master/vm/user-bootstrap.sh
国人写的参考:
-
https://www.sdnlab.com/22512.html;
-
https://blog.csdn.net/father_is_/article/details/108225712?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.edu_weight&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.edu_weight
2.gRPC安装参考
1.组件介绍
对于目前的认知来说,需要安装五个组件,其作用分别是:
- bmv2:支持P4的软件交换机
- p4c:P4编译器,负责将P4程序编译为底层交换机可以理解的代码
- mininet:构建虚拟网络
- p4-tutotial:用于学习的示例代码
- PI:南向接口,用于控制器和交换机之间的通信
2.版本依赖
在linux中安装各类软件,非常令人痛苦的就是各类库之间存在依赖关系,并且会依赖于具体的版本。所以在决定安装软件之前,一定要清楚自己安装的是哪一个版本。参考官方文档 , 各版本都有明确的要求
3.安装
由于直接按照脚本安装,会出现错误,并且由于脚本中安装内容比较丰富,在出错的情况下难以快速定位,所以本文会一步步安装。同时建议读者将官方给的脚本分成多个部分,如mininet安装脚本,protobuf安装脚本等等
1.依赖项安装
这个阶段一般不会有问题,网速比较慢的话,建议将Ubuntu的更新源换成阿里或者网易
sudo apt-get update
sudo apt-get install automake cmake libjudy-dev libpcap-dev libboost-dev libboost-test-dev libboost-program-options-dev libboost-system-dev libboost-filesystem-dev libboost-thread-dev libevent-dev libtool flex bison pkg-config g++ libssl-dev -y
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 curl -y
sudo apt-get install libreadline6 libreadline6-dev python-pip -y
sudo pip install psutil
sudo pip install crcmod
2.Mininet安装
此阶段也未遇到问题
#--- Mininet ---
git clone git://github.com/mininet/mininet mininet
sudo ./mininet/util/install.sh -nwv
3.Protobuf安装
Protobuf是一种数据序列化工具,后文gRPC通信需要依赖protobuf。
PROTOBUF_COMMIT="v3.2.0"
#Get the number of cores to speed up the compilation process
NUM_CORES=`grep -c ^processor /proc/cpuinfo`
# git clone https://github.com/google/protobuf.git
git clone https://gitee.com/tonysw/protobuf.git
cd protobuf
git checkout ${PROTOBUF_COMMIT}
export CFLAGS="-Os"
export CXXFLAGS="-Os"
export LDFLAGS="-Wl,-s"
./autogen.sh
./configure --prefix=/usr
make -j${NUM_CORES}
sudo make install
sudo ldconfig
unset CFLAGS CXXFLAGS LDFLAGS
# Force install python module
cd python
sudo python setup.py install
cd ../..
如何验证protobuf安装成功
安装之后,运行
#查看版本
protoc --version
版本显示正确,则说明已正确安装
4.gRPC安装
其实在第一次安装gRPC的时候,遇到了很多问题,但是我在第二次纯净Ubuntu系统试图复现问题时,居然没有再遇到之前折腾很久的问题。所以只能按照记忆写下曾经遇到过的问题,希望给读者一些启发。
set -xe
GRPC_COMMIT="v1.3.2"
#Get the number of cores to speed up the compilation process
NUM_CORES=`grep -c ^processor /proc/cpuinfo`
# git clone https://github.com/grpc/grpc.git
git clone https://gitee.com/tonysw/grpc.git
cd grpc
git checkout ${GRPC_COMMIT}
git submodule update --init --recursive
问题1 无法获取子模块boringssl
这个问题达概率就是因为网络的问题,无法访问google的服务器。可以通过修改.gitmodules下的url。将不能下载的boringssl网址改为国内源https://gitee.com/tonysw/boringssl.git
之后继续输入
export LDFLAGS="-Wl,-s"
make -j${NUM_CORES}
sudo make install
sudo ldconfig
unset LDFLAGS
cd ..
# Install gRPC Python Package
sudo pip install grpcio
注意,这个时候已经不能直接运行脚本了。因为grpc从github上克隆成功,直接手动从export继续输入。
如何验证安装成功
利用gRPC自带的helloworld测试程序
cd examples/cpp/helloworld/
sudo make #如果此处出错,可能就是安装有问题了
sudo ./greeter_server #运行server,监听50051端口
#打开一个新的终端运行client
sudo ./greeter_client
#就可以看到返回结果:Greeter received: Hello world
5.Bmv2 依赖安装
我个人的理解这是architecture,也就是说bmv2是按照这个架构搭建的服务器,在这里被称为behavioraol-model。
set -xe
#Src
BMV2_COMMIT="b447ac4c0cfd83e5e72a3cc6120251c1e91128ab" # August 10, 2019
git clone https://github.com/p4lang/behavioral-model.git
cd behavioral-model
git checkout ${BMV2_COMMIT}
# From bmv2's install_deps.sh, we can skip apt-get install.
# Nanomsg is required by p4runtime, p4runtime is needed by BMv2...
tmpdir=`mktemp -d -p .`
cd ${tmpdir}
bash ../travis/install-thrift.sh
bash ../travis/install-nanomsg.sh
sudo ldconfig
bash ../travis/install-nnpy.sh
cd ..
sudo rm -rf $tmpdir
cd ..
6.PI安装
PI实际上就是南向接口,负责控制层和交换机之间的通信
# Print script commands and exit on errors.
set -xe
#Src
PI_COMMIT="41358da0ff32c94fa13179b9cee0ab597c9ccbcc" # August 10, 2019
#Get the number of cores to speed up the compilation process
NUM_CORES=`grep -c ^processor /proc/cpuinfo`
# PI/P4Runtime
git clone https://github.com/p4lang/PI.git
cd PI
git checkout ${PI_COMMIT}
git submodule update --init --recursive
./autogen.sh
./configure --with-proto
make -j${NUM_CORES}
sudo make install
sudo ldconfig
cd ..
7.Bmv2安装
正式安装bmv2
# --- Bmv2 --- #
cd behavioral-model
./autogen.sh
./configure --enable-debugger --with-pi
make -j${NUM_CORES}
sudo make install
sudo ldconfig
# Simple_switch_grpc target
cd targets/simple_switch_grpc
./autogen.sh
./configure --with-thrift
make -j${NUM_CORES}
sudo make install
sudo ldconfig
cd ../../..
8.p4c安装
#!/bin/bash
# Print script commands and exit on errors.
set -xe
#Src
P4C_COMMIT="69e132d0d663e3408d740aaf8ed534ecefc88810" # August 10, 2019
#Get the number of cores to speed up the compilation process
NUM_CORES=`grep -c ^processor /proc/cpuinfo`
# --- P4C --- #
git clone https://github.com/p4lang/p4c
cd p4c
git checkout ${P4C_COMMIT}
git submodule update --init --recursive
mkdir -p build
cd build
cmake ..
# The command 'make -j${NUM_CORES}' works fine for the others, but
# with 2 GB of RAM for the VM, there are parts of the p4c build where
# running 2 simultaneous C++ compiler runs requires more than that
# much memory. Things work better by running at most one C++ compilation
# process at a time.
make -j1
sudo make install
sudo ldconfig
cd ../..
9.tutorial安装
这一步也比较简单
git clone https://github.com/p4lang/tutorials
验证安装是否成功
#
#This will:
#compile basic.p4, and
#start a Mininet instance with three switches (s1, s2, s3) configured in a triangle, each connected to one host (h1, h2, and h3).
#The hosts are assigned IPs of 10.0.1.1, 10.0.2.2, and 10.0.3.3.
#
make run
#you should now see a Mininet command prompt. Open two terminals for h1 and h2, respectively:
mininet> xterm h1 h2
#Each host includes a small Python-based messaging client and server. In h2's xterm, start the server:
./receive.py
#In h1's xterm, send a message to h2:
./send.py 10.0.2.2 "P4 is cool"
#Type exit to leave each xterm and the Mininet command line. Then, to stop mininet:
make stop
#And to delete all pcaps, build files, and logs:
make clean
此时,可以再h2的xterm命令行中看到

在h1的xterm命令行中看到如下:

尽管在h2的终端并没有接收到信息,这是因为basic.p4并未对交换机中的转发逻辑进行设置,而这正是我们需要去解决的问题。
结束语
总体来说,P4的安装比较费时,并且由于P4仍然在开发过程中,所以会存在很多问题。一定要有耐心呀。