|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +THIS_DIR=$( cd -- "$( dirname -- "${0}" )" &> /dev/null && pwd ) |
| 4 | +P4C_DIR=$(readlink -f ${THIS_DIR}/..) |
| 5 | + |
| 6 | +# Get distribution info |
| 7 | +DISTRO=$(lsb_release -i | awk -F: '{print $2}' | xargs) |
| 8 | +VERSION=$(lsb_release -r | awk -F: '{print $2}' | xargs) |
| 9 | + |
| 10 | +# Define supported distributions and versions |
| 11 | +SUPPORTED_DISTROS=("Ubuntu" "Pop") |
| 12 | +SUPPORTED_UBUNTU_VERSIONS=("20.04" "22.04" "24.04") |
| 13 | + |
| 14 | +# Function to check if an element exists in an array |
| 15 | +contains() { |
| 16 | + local element |
| 17 | + for element in "${@:2}"; do |
| 18 | + if [[ "$element" == "$1" ]]; then |
| 19 | + return 0 |
| 20 | + fi |
| 21 | + done |
| 22 | + return 1 |
| 23 | +} |
| 24 | + |
| 25 | +# Check distribution and version |
| 26 | +if contains "$DISTRO" "${SUPPORTED_DISTROS[@]}"; then |
| 27 | + if [[ "$DISTRO" == "Ubuntu" ]]; then |
| 28 | + if contains "$VERSION" "${SUPPORTED_UBUNTU_VERSIONS[@]}"; then |
| 29 | + echo "Supported distribution: $DISTRO $VERSION" |
| 30 | + exit 0 |
| 31 | + else |
| 32 | + echo "Error: Unsupported Ubuntu version: $VERSION" |
| 33 | + exit 1 |
| 34 | + fi |
| 35 | + else |
| 36 | + # Assume all versions of Pop!_OS and Linux Mint are supported |
| 37 | + echo "Supported distribution: $DISTRO $VERSION" |
| 38 | + fi |
| 39 | +else |
| 40 | + echo "Error: Unsupported distribution: $DISTRO" |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | + |
| 44 | +# Whether to install dependencies required to run PTF-ebpf tests |
| 45 | +: "${INSTALL_PTF_EBPF_DEPENDENCIES:=OFF}" |
| 46 | +# The build generator to use. Defaults to Make. |
| 47 | +: "${BUILD_GENERATOR:="ninja"}" |
| 48 | +# BMv2 is enable by default. |
| 49 | +: "${ENABLE_BMV2:=ON}" |
| 50 | +# eBPF is enabled by default. |
| 51 | +: "${ENABLE_EBPF:=ON}" |
| 52 | +# This is the list of back ends that can be enabled. |
| 53 | +# Back ends can be enabled from the command line with "ENABLE_[backend]=TRUE/FALSE" |
| 54 | +ENABLE_BACKENDS=("TOFINO" "BMV2" "EBPF" "UBPF" "DPDK" |
| 55 | + "P4TC" "P4FMT" "P4TEST" "P4C_GRAPHS" |
| 56 | + "TEST_TOOLS" |
| 57 | +) |
| 58 | +function build_cmake_enabled_backend_string() { |
| 59 | + CMAKE_ENABLE_BACKENDS="" |
| 60 | + for backend in "${ENABLE_BACKENDS[@]}"; |
| 61 | + do |
| 62 | + enable_var=ENABLE_${backend} |
| 63 | + if [ -n "${!enable_var}" ]; then |
| 64 | + echo "${enable_var}=${!enable_var} is set." |
| 65 | + CMAKE_ENABLE_BACKENDS+="-D${enable_var}=${!enable_var} " |
| 66 | + fi |
| 67 | + done |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +. /etc/lsb-release |
| 72 | + |
| 73 | +# In Docker builds, sudo is not available. So make it a noop. |
| 74 | +if [ "$IN_DOCKER" == "TRUE" ]; then |
| 75 | + echo "Executing within docker container." |
| 76 | + function sudo() { command "$@"; } |
| 77 | +fi |
| 78 | + |
| 79 | + |
| 80 | +# ! ------ BEGIN CORE ----------------------------------------------- |
| 81 | +P4C_DEPS="bison \ |
| 82 | + flex \ |
| 83 | + cmake \ |
| 84 | + build-essential \ |
| 85 | + ccache \ |
| 86 | + ninja-build \ |
| 87 | + g++ \ |
| 88 | + git \ |
| 89 | + lld \ |
| 90 | + libboost-graph-dev \ |
| 91 | + libboost-iostreams-dev \ |
| 92 | + libfl-dev \ |
| 93 | + python3 \ |
| 94 | + python3-pip \ |
| 95 | + python3-setuptools" |
| 96 | + |
| 97 | +sudo apt-get update |
| 98 | +sudo apt-get install -y --no-install-recommends ${P4C_DEPS} |
| 99 | + |
| 100 | +sudo apt-get install -y python3-poetry python3-venv |
| 101 | +poetry install |
| 102 | +poetry shell |
| 103 | + |
| 104 | +if [ "${BUILD_GENERATOR,,}" == "ninja" ] && [ ! $(command -v ninja) ] |
| 105 | +then |
| 106 | + echo "Selected ninja as build generator, but ninja could not be found." |
| 107 | + exit 1 |
| 108 | +fi |
| 109 | + |
| 110 | +# ! ------ END CORE ----------------------------------------------- |
| 111 | + |
| 112 | +ccache --set-config max_size=1G |
| 113 | + |
| 114 | +# ! ------ BEGIN BMV2 ----------------------------------------------- |
| 115 | +function build_bmv2() { |
| 116 | + P4C_RUNTIME_DEPS_BOOST="libboost-graph1.7* libboost-iostreams1.7*" |
| 117 | + |
| 118 | + P4C_RUNTIME_DEPS="cpp \ |
| 119 | + ${P4C_RUNTIME_DEPS_BOOST} \ |
| 120 | + libgc1* \ |
| 121 | + libgmp-dev \ |
| 122 | + libnanomsg-dev" |
| 123 | + |
| 124 | + sudo apt-get install -y wget ca-certificates |
| 125 | + # Add the p4lang opensuse repository. |
| 126 | + echo "deb http://download.opensuse.org/repositories/home:/p4lang/xUbuntu_${DISTRIB_RELEASE}/ /" | sudo tee /etc/apt/sources.list.d/home:p4lang.list |
| 127 | + curl -fsSL https://download.opensuse.org/repositories/home:p4lang/xUbuntu_${DISTRIB_RELEASE}/Release.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/home_p4lang.gpg > /dev/null |
| 128 | + P4C_RUNTIME_DEPS+=" p4lang-bmv2" |
| 129 | + |
| 130 | + sudo apt-get update && sudo apt-get install -y --no-install-recommends ${P4C_RUNTIME_DEPS} |
| 131 | + |
| 132 | +} |
| 133 | + |
| 134 | +if [[ "${ENABLE_BMV2}" == "ON" ]] ; then |
| 135 | + build_bmv2 |
| 136 | +fi |
| 137 | +# ! ------ END BMV2 ----------------------------------------------- |
| 138 | + |
| 139 | +# ! ------ BEGIN EBPF ----------------------------------------------- |
| 140 | +function build_ebpf() { |
| 141 | + P4C_EBPF_DEPS="libpcap-dev \ |
| 142 | + libelf-dev \ |
| 143 | + zlib1g-dev \ |
| 144 | + llvm \ |
| 145 | + clang \ |
| 146 | + tcpdump \ |
| 147 | + iproute2 \ |
| 148 | + iptables \ |
| 149 | + net-tools" |
| 150 | + |
| 151 | + sudo apt-get install -y --no-install-recommends ${P4C_EBPF_DEPS} |
| 152 | +} |
| 153 | + |
| 154 | +function install_ptf_ebpf_test_deps() ( |
| 155 | + P4C_PTF_PACKAGES="gcc-multilib \ |
| 156 | + python3-six \ |
| 157 | + libgmp-dev \ |
| 158 | + libjansson-dev" |
| 159 | + sudo apt-get install -y --no-install-recommends ${P4C_PTF_PACKAGES} |
| 160 | + |
| 161 | + git clone --depth 1 --recursive --branch v0.3.1 https://github.com/NIKSS-vSwitch/nikss /tmp/nikss |
| 162 | + pushd /tmp/nikss |
| 163 | + ./build_libbpf.sh |
| 164 | + mkdir build |
| 165 | + cd build |
| 166 | + cmake -DCMAKE_BUILD_TYPE=Release -G "${BUILD_GENERATOR}" .. |
| 167 | + cmake --build . -- -j $(nproc) |
| 168 | + sudo cmake --install . |
| 169 | + |
| 170 | + # install bpftool |
| 171 | + git clone --recurse-submodules --branch v7.3.0 https://github.com/libbpf/bpftool.git /tmp/bpftool |
| 172 | + cd /tmp/bpftool/src |
| 173 | + make "-j$(nproc)" |
| 174 | + sudo make install |
| 175 | + popd |
| 176 | +) |
| 177 | + |
| 178 | +if [[ "${ENABLE_EBPF}" == "ON" ]] ; then |
| 179 | + build_ebpf |
| 180 | + if [[ "${INSTALL_PTF_EBPF_DEPENDENCIES}" == "ON" ]] ; then |
| 181 | + install_ptf_ebpf_test_deps |
| 182 | + fi |
| 183 | +fi |
| 184 | +# ! ------ END EBPF ----------------------------------------------- |
| 185 | + |
| 186 | +# ! ------ BEGIN DPDK ----------------------------------------------- |
| 187 | +function build_dpdk() { |
| 188 | + # Replace existing Protobuf with one that works. |
| 189 | + # TODO: Debug protobuf mismatch. |
| 190 | + sudo -E pip3 uninstall -y protobuf |
| 191 | + sudo pip3 install protobuf==3.20.3 netaddr==0.9.0 |
| 192 | +} |
| 193 | + |
| 194 | +if [[ "${ENABLE_DPDK}" == "ON" ]]; then |
| 195 | + build_dpdk |
| 196 | +fi |
| 197 | +# ! ------ END DPDK ----------------------------------------------- |
| 198 | + |
| 199 | +# ! ------ BEGIN TOFINO -------------------------------------------- |
| 200 | + |
| 201 | +function build_tofino() { |
| 202 | + P4C_TOFINO_PACKAGES="rapidjson-dev" |
| 203 | + sudo apt-get install -y --no-install-recommends ${P4C_TOFINO_PACKAGES} |
| 204 | + sudo pip3 install jsl==0.2.4 pyinstaller==6.11.0 |
| 205 | +} |
| 206 | + |
| 207 | +if [[ "${ENABLE_TOFINO}" == "ON" ]]; then |
| 208 | + echo "Installing Tofino dependencies" |
| 209 | + build_tofino |
| 210 | +fi |
| 211 | +# ! ------ END TOFINO ---------------------------------------------- |
| 212 | + |
| 213 | +echo "Test image ready" |
| 214 | + |
0 commit comments