blob: b1247519f135e4debea744f8ec53ea935753aca1 [file] [log] [blame]
Carmelo Casconeb7e618d2018-01-12 18:31:33 -08001#!/usr/bin/env bash
2# -----------------------------------------------------------------------------
3# Builds and installs all tools needed for developing and testing P4 support in
4# ONOS.
5#
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -08006# Tested on 16.04 and 18.04.
Carmelo Casconeb7e618d2018-01-12 18:31:33 -08007#
8# Recommended minimum system requirements:
9# 4 GB of RAM
10# 2 cores
11# 8 GB free hard drive space (~4 GB to build everything)
Carmelo Cascone95e5afd2018-07-17 14:45:23 +020012#
13# To execute up to a given step, pass the step name as the first argument. For
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -080014# example, to install PI, but not bmv2, p4c, etc:
Carmelo Cascone95e5afd2018-07-17 14:45:23 +020015# ./install-p4-tools.sh PI
Carmelo Casconeb7e618d2018-01-12 18:31:33 -080016# -----------------------------------------------------------------------------
17
18# Exit on errors.
19set -e
Carmelo Casconea4dc3c12019-02-12 17:30:00 -080020set -x
21
Carmelo Casconeada7b5b2019-04-23 13:50:03 -070022BMV2_COMMIT="99d83115d498f0ebcc729dad4fa4c50577eccc81"
Yi Tsengb81121f2019-05-07 16:51:05 -070023PI_COMMIT="f9a5c6c74f7dcde382e27c63af2fe5dffc755364"
Carmelo Casconeada7b5b2019-04-23 13:50:03 -070024P4C_COMMIT="e2934ab32ace8a877bf2b34704950a4da69b6202"
Carmelo Casconea4dc3c12019-02-12 17:30:00 -080025
26# p4c seems to break when using protobuf versions newer than 3.2.0
27PROTOBUF_VER=${PROTOBUF_VER:-3.2.0}
28GRPC_VER=${GRPC_VER:-1.3.2}
29
Carmelo Casconeb7e618d2018-01-12 18:31:33 -080030
31BUILD_DIR=~/p4tools
Carmelo Casconeb7e618d2018-01-12 18:31:33 -080032NUM_CORES=`grep -c ^processor /proc/cpuinfo`
Carmelo Casconef02872d2018-06-20 08:49:02 +020033# If false, build tools without debug features to improve throughput of BMv2 and
Carmelo Cascone03ae0ac2018-10-11 08:31:59 -070034# reduce CPU/memory footprint. Default is true.
Carmelo Casconef02872d2018-06-20 08:49:02 +020035DEBUG_FLAGS=${DEBUG_FLAGS:-true}
Carmelo Cascone95e5afd2018-07-17 14:45:23 +020036# Execute up to the given step (first argument), or all if not defined.
37LAST_STEP=${1:-all}
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -080038# PI and BMv2 must be configured differently if we want to use Stratum
39USE_STRATUM=${USE_STRATUM:-false}
40# Improve time for one-time builds
41FAST_BUILD=${FAST_BUILD:-false}
42# Remove build artifacts
43CLEAN_UP=${CLEAN_UP:-false}
44BMV2_INSTALL=/usr/local
45set +x
Carmelo Cascone95e5afd2018-07-17 14:45:23 +020046
Carmelo Casconeb7e618d2018-01-12 18:31:33 -080047function do_requirements {
48 sudo apt update
49 sudo apt-get install -y --no-install-recommends \
50 autoconf \
51 automake \
52 bison \
53 build-essential \
54 cmake \
55 cpp \
56 curl \
57 flex \
58 git \
Carmelo Casconeb5324e72018-11-25 02:26:32 -080059 graphviz \
Carmelo Casconeb7e618d2018-01-12 18:31:33 -080060 libavl-dev \
61 libboost-dev \
Carmelo Casconeb5324e72018-11-25 02:26:32 -080062 libboost-graph-dev \
Carmelo Casconeb7e618d2018-01-12 18:31:33 -080063 libboost-program-options-dev \
64 libboost-system-dev \
65 libboost-filesystem-dev \
66 libboost-thread-dev \
67 libboost-filesystem-dev \
68 libboost-program-options-dev \
69 libboost-system-dev \
70 libboost-test-dev \
71 libboost-thread-dev \
72 libc6-dev \
73 libev-dev \
74 libevent-dev \
75 libffi-dev \
76 libfl-dev \
77 libgc-dev \
78 libgc1c2 \
79 libgflags-dev \
80 libgmp-dev \
81 libgmp10 \
82 libgmpxx4ldbl \
83 libjudy-dev \
84 libpcap-dev \
85 libpcre3-dev \
Carmelo Casconeb7e618d2018-01-12 18:31:33 -080086 libssl-dev \
87 libtool \
88 make \
Carmelo Casconeb7e618d2018-01-12 18:31:33 -080089 pkg-config \
Carmelo Cascone57defd32018-05-11 14:34:01 -070090 python2.7 \
91 python2.7-dev \
Carmelo Casconeb7e618d2018-01-12 18:31:33 -080092 tcpdump \
93 wget \
94 unzip
95
Carmelo Casconeada7b5b2019-04-23 13:50:03 -070096 sudo -H pip2.7 install setuptools cffi ipaddr ipaddress pypcap \
97 git+https://github.com/p4lang/scapy-vxlan \
98 git+https://github.com/p4lang/ptf.git
Carmelo Casconeb7e618d2018-01-12 18:31:33 -080099}
100
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800101function do_requirements_1604 {
102 sudo apt-get update
103 sudo apt-get install -y --no-install-recommends \
104 ca-certificates \
105 g++ \
Kevin Chuang53a9d5b2018-01-17 15:55:32 +0800106 libboost-iostreams1.58-dev \
Jonghwan Hyunbfb3a212018-11-16 11:42:18 +0900107 libreadline6 \
108 libreadline6-dev \
109 mktemp
110}
111
112function do_requirements_1804 {
113 sudo apt-get update
114 sudo apt-get install -y --no-install-recommends \
115 ca-certificates \
116 g++ \
117 libboost1.65-dev \
118 libboost-regex1.65-dev \
119 libboost-iostreams1.65-dev \
120 libreadline-dev \
Carmelo Casconea4dc3c12019-02-12 17:30:00 -0800121 libssl1.0-dev
Kevin Chuang53a9d5b2018-01-17 15:55:32 +0800122}
123
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800124function do_protobuf {
125 cd ${BUILD_DIR}
Carmelo Casconea4dc3c12019-02-12 17:30:00 -0800126 if [[ ! -d protobuf-${PROTOBUF_VER} ]]; then
127 # Get python package which also includes cpp.
128 wget https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOBUF_VER}/protobuf-python-${PROTOBUF_VER}.tar.gz
129 tar -xzf protobuf-python-${PROTOBUF_VER}.tar.gz
130 rm -r protobuf-python-${PROTOBUF_VER}.tar.gz
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800131 fi
Carmelo Casconea4dc3c12019-02-12 17:30:00 -0800132 cd protobuf-${PROTOBUF_VER}
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800133
134 export CFLAGS="-Os"
135 export CXXFLAGS="-Os"
136 export LDFLAGS="-Wl,-s"
137 ./autogen.sh
Carmelo Casconea4dc3c12019-02-12 17:30:00 -0800138 confOpts="--prefix=/usr"
139 if [[ "${FAST_BUILD}" = true ]] ; then
140 confOpts="${confOpts} --disable-dependency-tracking"
141 fi
142 ./configure ${confOpts}
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800143 make -j${NUM_CORES}
144 sudo make install
145 sudo ldconfig
146 unset CFLAGS CXXFLAGS LDFLAGS
Carmelo Casconef645e842018-07-16 18:31:52 +0200147
148 cd python
Carmelo Casconea4dc3c12019-02-12 17:30:00 -0800149 # Hack to get the -std=c++11 flag when building 3.6.1
150 # https://github.com/protocolbuffers/protobuf/blob/v3.6.1/python/setup.py#L208
151 export KOKORO_BUILD_NUMBER="hack"
Carmelo Casconeada7b5b2019-04-23 13:50:03 -0700152 sudo -E python2.7 setup.py build --cpp_implementation
153 sudo -E pip2.7 install .
Carmelo Casconea4dc3c12019-02-12 17:30:00 -0800154 unset KOKORO_BUILD_NUMBER
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800155}
156
157function do_grpc {
158 cd ${BUILD_DIR}
Carmelo Casconea4dc3c12019-02-12 17:30:00 -0800159 if [[ ! -d grpc-${GRPC_VER} ]]; then
160 git clone --depth 1 --single-branch --branch v${GRPC_VER} https://github.com/grpc/grpc.git grpc-${GRPC_VER}
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800161 fi
Carmelo Casconea4dc3c12019-02-12 17:30:00 -0800162
163 cd grpc-${GRPC_VER}
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800164 git submodule update --init
165
166 export LDFLAGS="-Wl,-s"
Jonghwan Hyunbfb3a212018-11-16 11:42:18 +0900167 RELEASE=`lsb_release -rs`
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800168 if version_ge ${RELEASE} 18.04; then
Jonghwan Hyunbfb3a212018-11-16 11:42:18 +0900169 # Ubuntu 18.04 ships OpenSSL 1.1 by default, which has breaking changes in the API.
170 # Here, we will build grpc with OpenSSL 1.0.
171 # (Reference: https://github.com/grpc/grpc/issues/10589)
172 # Also, set CFLAGS to avoid compilcation error caused by gcc7.
173 # (Reference: https://github.com/grpc/grpc/issues/13854)
174 PKG_CONFIG_PATH=/usr/lib/openssl-1.0/pkgconfig make -j${NUM_CORES} CFLAGS='-Wno-error'
175 else
176 make -j${NUM_CORES}
177 fi
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800178 sudo make install
179 sudo ldconfig
180 unset LDFLAGS
Carmelo Casconef645e842018-07-16 18:31:52 +0200181
Carmelo Casconeada7b5b2019-04-23 13:50:03 -0700182 sudo pip2.7 install -r requirements.txt
183 sudo pip2.7 install .
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800184}
185
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800186function checkout_bmv2 {
187 cd ${BUILD_DIR}
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800188 if [[ ! -d bmv2 ]]; then
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800189 git clone https://github.com/p4lang/behavioral-model.git bmv2
190 fi
191 cd bmv2
192 git fetch
193 git checkout ${BMV2_COMMIT}
194}
195
196function do_pi_bmv2_deps {
197 checkout_bmv2
198 # From bmv2's install_deps.sh.
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800199 tmpdir=`mktemp -d -p .`
200 cd ${tmpdir}
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800201 if [[ "${USE_STRATUM}" = false ]] ; then
202 bash ../travis/install-thrift.sh
203 fi
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800204 sudo ldconfig
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800205 cd ..
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800206 sudo rm -rf ${tmpdir}
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800207}
208
Carmelo Cascone95e5afd2018-07-17 14:45:23 +0200209function do_PI {
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800210 cd ${BUILD_DIR}
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800211 if [[ ! -d PI ]]; then
Carmelo Cascone95e5afd2018-07-17 14:45:23 +0200212 git clone https://github.com/p4lang/PI.git
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800213 fi
Carmelo Cascone95e5afd2018-07-17 14:45:23 +0200214 cd PI
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800215 git fetch
216 git checkout ${PI_COMMIT}
217 git submodule update --init --recursive
218
219 ./autogen.sh
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800220 if [[ "${USE_STRATUM}" = false ]] ; then
221 ./configure --with-proto --without-internal-rpc --without-cli
222 else
223 # Configure for Stratum
224 ./configure --without-bmv2 --without-proto --without-fe-cpp --without-cli --without-internal-rpc --prefix=${BMV2_INSTALL}
225 fi
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800226 make -j${NUM_CORES}
227 sudo make install
228 sudo ldconfig
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800229}
230
231function do_bmv2 {
232 checkout_bmv2
233
234 ./autogen.sh
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800235
236 confOpts="--with-pi --disable-elogger --without-nanomsg --without-targets"
Carmelo Cascone4b2ff042019-04-16 13:25:10 -0700237 # We are building --without-targets but we know for sure we need those
238 # parts of the BMv2 PI library for simple_switch.
239 cppFlags="-I${PWD}/targets/simple_switch -DWITH_SIMPLE_SWITCH"
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800240 if [[ "${FAST_BUILD}" = true ]] ; then
241 confOpts="${confOpts} --disable-dependency-tracking"
Carmelo Casconef02872d2018-06-20 08:49:02 +0200242 fi
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800243 if [[ "${DEBUG_FLAGS}" = false ]] ; then
244 confOpts="${confOpts} --disable-logging-macros"
245 fi
246 if [[ "${USE_STRATUM}" = true ]] ; then
Carmelo Cascone4b2ff042019-04-16 13:25:10 -0700247 confOpts="--prefix=${BMV2_INSTALL} --without-thrift ${confOpts}"
248 cppFlags="${cppFlags} -isystem${BMV2_INSTALL}/include"
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800249 fi
Carmelo Cascone4b2ff042019-04-16 13:25:10 -0700250 confCmd="./configure CPPFLAGS=\"${cppFlags}\" ${confOpts}"
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800251 eval ${confCmd}
252
253 make -j${NUM_CORES}
254 sudo make install
255 cd targets/simple_switch
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800256 make -j${NUM_CORES}
257 sudo make install
258 sudo ldconfig
259
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800260 if [[ "${USE_STRATUM}" = false ]] ; then
261 # Simple_switch_grpc target (not using Stratum)
Carmelo Casconea4dc3c12019-02-12 17:30:00 -0800262 cd ../simple_switch_grpc
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800263 ./autogen.sh
264 ./configure --with-thrift
265 make -j${NUM_CORES}
266 sudo make install
267 sudo ldconfig
268 fi
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800269}
270
271function do_p4c {
272 cd ${BUILD_DIR}
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800273 if [[ ! -d p4c ]]; then
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800274 git clone https://github.com/p4lang/p4c.git
275 fi
276 cd p4c
277 git fetch
278 git checkout ${P4C_COMMIT}
279 git submodule update --init --recursive
280
281 mkdir -p build
282 cd build
Carmelo Cascone03ae0ac2018-10-11 08:31:59 -0700283 cmake .. -DENABLE_EBPF=OFF
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800284 make -j${NUM_CORES}
285 sudo make install
286 sudo ldconfig
287}
288
289function check_commit {
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800290 if [[ ! -e $2 ]]; then
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800291 return 0 # true
292 fi
293 if [[ $(< $2) != "$1" ]]; then
294 return 0 # true
295 fi
296 return 1 # false
297}
298
299# The following is borrowed from Mininet's util/install.sh
300function version_ge {
301 # sort -V sorts by *version number*
302 latest=`printf "$1\n$2" | sort -V | tail -1`
303 # If $1 is latest version, then $1 >= $2
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800304 [[ "$1" == "$latest" ]]
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800305}
306
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800307function missing_lib {
Carmelo Casconee45902b2018-12-18 13:30:45 -0800308 ldconfig -p | grep $1 &> /dev/null
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800309 if [[ $? == 0 ]]; then
Carmelo Casconee45902b2018-12-18 13:30:45 -0800310 echo "$1 found!"
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800311 return 1 # false
Carmelo Casconee45902b2018-12-18 13:30:45 -0800312 fi
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800313 return 0 # true
314}
315
Carmelo Casconea4dc3c12019-02-12 17:30:00 -0800316function missing_protoc {
317 command -v protoc >/dev/null 2>&1
318 if [[ $? == 0 ]]; then
319 protoc --version | grep $1 &> /dev/null
320 if [[ $? == 0 ]]; then
321 echo "protoc ${1} found!"
322 else
323 echo "A version of protoc was found, but not $1 (you may experience issues)"
324 fi
325 return 1 # false
326 fi
327 return 0 # true
328}
329
330function missing_grpc {
331 # Is there a better way to check if a specific version of grpc is installed?
332 if [[ -f /usr/local/lib/libgrpc++.so ]]; then
333 ls -l /usr/local/lib/libgrpc++.so | grep libgrpc++.so.${1} &> /dev/null
334 if [[ $? == 0 ]]; then
335 echo "grpc ${1} found!"
336 else
337 echo "A version of grpc was found, but not $1 (you may experience issues)"
338 fi
339 return 1 # false
340 else
341 return 0 # true
342 fi
343}
344
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800345function all_done {
346 if [[ "${CLEAN_UP}" = true ]] ; then
347 echo "Cleaning up build dir... ${BUILD_DIR})"
348 sudo rm -rf ${BUILD_DIR}
349 fi
350 echo "Done!"
351 exit 0
Carmelo Casconee45902b2018-12-18 13:30:45 -0800352}
353
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800354MUST_DO_ALL=false
355DID_REQUIREMENTS=false
356function check_and_do {
357 # Check if the latest built commit is the same we are trying to build now,
358 # or if all projects must be built. If true builds this project.
359 commit_id="$1"
360 proj_dir="$2"
361 func_name="$3"
Carmelo Cascone95e5afd2018-07-17 14:45:23 +0200362 step_name="$4"
363 commit_file=${BUILD_DIR}/${proj_dir}/.last_built_commit_${step_name}
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800364 if [[ ${MUST_DO_ALL} = true ]] \
Carmelo Cascone95e5afd2018-07-17 14:45:23 +0200365 || check_commit ${commit_id} ${commit_file}; then
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800366 echo "#"
Carmelo Cascone95e5afd2018-07-17 14:45:23 +0200367 echo "# Building ${step_name} (${commit_id})"
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800368 echo "#"
369 # Print commands used to install to aid debugging
370 set -x
371 if ! ${DID_REQUIREMENTS} = true; then
372 do_requirements
373 # TODO consider other Linux distros; presently this script assumes
374 # that it is running on Ubuntu.
375 RELEASE=`lsb_release -rs`
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800376 if version_ge ${RELEASE} 18.04; then
Jonghwan Hyunbfb3a212018-11-16 11:42:18 +0900377 do_requirements_1804
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800378 elif version_ge ${RELEASE} 16.04; then
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800379 do_requirements_1604
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800380 else
381 echo "Ubuntu version $RELEASE is not supported"
382 exit 1
383 fi
384 DID_REQUIREMENTS=true
385 fi
386 eval ${func_name}
Carmelo Casconee45902b2018-12-18 13:30:45 -0800387 if [[ -d ${BUILD_DIR}/${proj_dir} ]]; then
388 # If project was built, we expect its dir. Otherwise, we assume
389 # build was skipped.
390 echo ${commit_id} > ${commit_file}
391 # Build all next projects as they might depend on this one.
392 MUST_DO_ALL=true
393 fi
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800394 # Disable printing to reduce output
395 set +x
396 else
Carmelo Cascone95e5afd2018-07-17 14:45:23 +0200397 echo "${step_name} is up to date (commit ${commit_id})"
398 fi
399 # Exit if last step.
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800400 if [[ ${step_name} = ${LAST_STEP} ]]; then
401 all_done
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800402 fi
403}
404
405mkdir -p ${BUILD_DIR}
406cd ${BUILD_DIR}
407# In dependency order.
Carmelo Casconea4dc3c12019-02-12 17:30:00 -0800408if missing_protoc ${PROTOBUF_VER}; then
409 check_and_do ${PROTOBUF_VER} protobuf-${PROTOBUF_VER} do_protobuf protobuf
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800410fi
Carmelo Casconea4dc3c12019-02-12 17:30:00 -0800411if missing_grpc ${GRPC_VER}; then
412 check_and_do ${GRPC_VER} grpc-${GRPC_VER} do_grpc grpc
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800413fi
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800414check_and_do ${BMV2_COMMIT} bmv2 do_pi_bmv2_deps bmv2-deps
Carmelo Cascone95e5afd2018-07-17 14:45:23 +0200415check_and_do ${PI_COMMIT} PI do_PI PI
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800416check_and_do ${BMV2_COMMIT} bmv2 do_bmv2 bmv2
417check_and_do ${P4C_COMMIT} p4c do_p4c p4c
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800418
Carmelo Cascone7c82bcf2019-02-08 22:57:18 -0800419all_done