blob: b2ce8c7a3ac9a168e63b0bb5309d300498e4bc4e [file] [log] [blame]
Carmelo Cascone97efefb2019-12-05 16:40:45 -08001#!/usr/bin/env bash
2# Copyright 2019-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16MAVERICKS_CPU_PORT=320
17MONTARA_CPU_PORT=192
18
19if [ -z "$ONOS_ROOT" ]; then
20 echo "Error: ONOS_ROOT is not set"
21 exit 1
22fi
23
24P4_SRC_DIR=${ONOS_ROOT}/pipelines/fabric/impl/src/main/resources
25
26if [ ! -d "${P4_SRC_DIR}" ]; then
27 echo "Error: unable to locate fabric P4 sources at ${P4_SRC_DIR}"
28fi
29
30set -e
31
32PROFILE=$1
33OTHER_PP_FLAGS=$2
34
35# PWD is the directory where this script is called from (should be the root of
36# this repo).
Carmelo Cascone91d92fb2019-12-16 18:34:26 -080037P4C_OUT=${PWD}/tmp/${PROFILE}
pierbd6eaf02020-06-05 20:45:26 +020038# Prevent the creation by docker run
39mkdir -p ${P4C_OUT}
Carmelo Cascone97efefb2019-12-05 16:40:45 -080040
41# DIR is this file directory.
42DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
43
44# Where the compiler output should be placed to be included in the pipeconf.
45DEST_DIR=${DIR}/../resources/p4c-out/${PROFILE}/tofino
46
47# If SDE_DOCKER_IMG env is set, use containerized version of the compiler
48if [ -z "${SDE_DOCKER_IMG}" ]; then
49 P4C_CMD="bf-p4c"
50else
51 P4C_CMD="docker run --rm -v ${P4C_OUT}:${P4C_OUT} -v ${P4_SRC_DIR}:${P4_SRC_DIR} -v ${DIR}:${DIR} -w ${DIR} ${SDE_DOCKER_IMG} bf-p4c"
52fi
53
54SDE_VER=$( ${P4C_CMD} --version | cut -d' ' -f2 )
55
56# shellcheck disable=SC2086
57function do_p4c() {
58 pltf="$1_sde_${SDE_VER//./_}"
59 cpu_port=$2
60 echo "*** Compiling profile '${PROFILE}' for ${pltf} platform..."
61 echo "*** Output in ${P4C_OUT}/${pltf}"
62 pp_flags="-DCPU_PORT=${cpu_port}"
Daniele Moro2ecec232020-01-16 11:10:51 -080063 p4c_flags="--auto-init-metadata"
Carmelo Cascone97efefb2019-12-05 16:40:45 -080064 mkdir -p ${P4C_OUT}/${pltf}
65 (
66 set -x
Carmelo Cascone91d92fb2019-12-16 18:34:26 -080067 $P4C_CMD --arch v1model -g --create-graphs --verbose 2 \
Carmelo Cascone97efefb2019-12-05 16:40:45 -080068 -o ${P4C_OUT}/${pltf} -I ${P4_SRC_DIR} \
69 ${pp_flags} ${OTHER_PP_FLAGS} \
Daniele Moro2ecec232020-01-16 11:10:51 -080070 ${p4c_flags} \
Carmelo Cascone97efefb2019-12-05 16:40:45 -080071 --p4runtime-files ${P4C_OUT}/${pltf}/p4info.txt \
72 ${DIR}/fabric-tofino.p4
73 )
74
75 # Copy only the relevant files to the pipeconf resources
76 mkdir -p ${DEST_DIR}/${pltf}/pipe
77 cp ${P4C_OUT}/${pltf}/p4info.txt ${DEST_DIR}/${pltf}
78 cp ${P4C_OUT}/${pltf}/pipe/context.json ${DEST_DIR}/${pltf}/pipe
79 cp ${P4C_OUT}/${pltf}/pipe/tofino.bin ${DEST_DIR}/${pltf}/pipe
80 echo "${cpu_port}" > ${DEST_DIR}/${pltf}/cpu_port.txt
81
82 echo
83}
84
85do_p4c "mavericks" ${MAVERICKS_CPU_PORT}
86do_p4c "montara" ${MONTARA_CPU_PORT}