Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 2 | |
| 3 | usage () { |
Quentin Monnet | 9ba94fd | 2022-01-16 00:04:19 +0000 | [diff] [blame] | 4 | echo "USAGE: ./sync-kernel.sh <bpftool-repo> <kernel-repo>" |
| 5 | echo "" |
| 6 | echo "This script synchronizes the mirror with upstream bpftool sources from the kernel repository." |
| 7 | echo "It performs the following steps:" |
| 8 | echo " - Update the libbpf submodule, commit, and use its new checkpoints as target commits for bpftool." |
| 9 | echo " - Cherry-pick commits from the bpf-next branch, up to the bpf-next target commit." |
| 10 | echo " - Cherry-pick commits from the bpf branch, up to the bpf target commit." |
Quentin Monnet | 9ba94fd | 2022-01-16 00:04:19 +0000 | [diff] [blame] | 11 | echo " - Create a new commit with the updated version and checkpoints." |
| 12 | echo " - Check consistency." |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 13 | echo "" |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 14 | echo "Set BPF_NEXT_BASELINE to override bpf-next tree commit, otherwise read from <bpftool-repo>/CHECKPOINT-COMMIT." |
| 15 | echo "Set BPF_BASELINE to override bpf tree commit, otherwise read from <bpftool-repo>/BPF-CHECKPOINT-COMMIT." |
Quentin Monnet | 9ba94fd | 2022-01-16 00:04:19 +0000 | [diff] [blame] | 16 | echo "Set BPF_NEXT_TIP_COMMIT to override bpf-next tree target commit, otherwise read from <bpftool-repo>/libbpf/CHECKPOINT-COMMIT, after libbpf update." |
| 17 | echo "Set BPF_TIP_COMMIT to override bpf tree target commit, otherwise read from <bpftool-repo>/libbpf/BPF-CHECKPOINT-COMMIT, after libbpf update." |
| 18 | echo "Set SKIP_LIBBPF_UPDATE to 1 to avoid updating libbpf automatically." |
| 19 | echo "Set MANUAL_MODE to 1 to manually control every cherry-picked commit." |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 20 | exit 1 |
| 21 | } |
| 22 | |
| 23 | set -eu |
| 24 | |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 25 | BPFTOOL_REPO=${1-""} |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 26 | LINUX_REPO=${2-""} |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 27 | |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 28 | if [ -z "${BPFTOOL_REPO}" ] || [ -z "${LINUX_REPO}" ]; then |
| 29 | echo "Error: bpftool or linux repos are not specified" |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 30 | usage |
| 31 | fi |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 32 | |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 33 | BASELINE_COMMIT=${BPF_NEXT_BASELINE:-$(cat "${BPFTOOL_REPO}"/CHECKPOINT-COMMIT)} |
| 34 | BPF_BASELINE_COMMIT=${BPF_BASELINE:-$(cat "${BPFTOOL_REPO}"/BPF-CHECKPOINT-COMMIT)} |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 35 | |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 36 | if [ -z "${BASELINE_COMMIT}" ] || [ -z "${BPF_BASELINE_COMMIT}" ]; then |
| 37 | echo "Error: bpf or bpf-next baseline commits are not provided" |
| 38 | usage |
| 39 | fi |
| 40 | |
| 41 | SUFFIX=$(date --utc +%Y-%m-%dT%H-%M-%S.%3NZ) |
| 42 | WORKDIR=$(pwd) |
| 43 | TMP_DIR=$(mktemp -d) |
| 44 | |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 45 | # shellcheck disable=SC2064 |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 46 | trap "cd ${WORKDIR}; exit" INT TERM EXIT |
| 47 | |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 48 | BPFTOOL_SRC_DIR="tools/bpf/bpftool" |
| 49 | |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 50 | declare -A PATH_MAP |
| 51 | PATH_MAP=( \ |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 52 | [${BPFTOOL_SRC_DIR}]=src \ |
| 53 | [${BPFTOOL_SRC_DIR}/bash-completion]=bash-completion \ |
| 54 | [${BPFTOOL_SRC_DIR}/Documentation]=docs \ |
| 55 | [kernel/bpf/disasm.c]=src/kernel/bpf/disasm.c \ |
| 56 | [kernel/bpf/disasm.h]=src/kernel/bpf/disasm.h \ |
Quentin Monnet | cc5caf6 | 2022-08-19 11:56:06 +0100 | [diff] [blame] | 57 | [tools/include/tools/dis-asm-compat.h]=include/tools/dis-asm-compat.h \ |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 58 | [tools/include/uapi/asm-generic/bitsperlong.h]=include/uapi/asm-generic/bitsperlong.h \ |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 59 | [tools/include/uapi/linux/bpf_common.h]=include/uapi/linux/bpf_common.h \ |
| 60 | [tools/include/uapi/linux/bpf.h]=include/uapi/linux/bpf.h \ |
| 61 | [tools/include/uapi/linux/btf.h]=include/uapi/linux/btf.h \ |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 62 | [tools/include/uapi/linux/const.h]=include/uapi/linux/const.h \ |
Quentin Monnet | a7f0e7f | 2022-01-27 20:29:45 +0000 | [diff] [blame] | 63 | [tools/include/uapi/linux/if_link.h]=include/uapi/linux/if_link.h \ |
James Hilliard | d5a59b2 | 2022-02-15 15:02:53 -0700 | [diff] [blame] | 64 | [tools/include/uapi/linux/netlink.h]=include/uapi/linux/netlink.h \ |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 65 | [tools/include/uapi/linux/perf_event.h]=include/uapi/linux/perf_event.h \ |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 66 | [tools/include/uapi/linux/pkt_cls.h]=include/uapi/linux/pkt_cls.h \ |
| 67 | [tools/include/uapi/linux/pkt_sched.h]=include/uapi/linux/pkt_sched.h \ |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 68 | [tools/include/uapi/linux/tc_act/tc_bpf.h]=include/uapi/linux/tc_act/tc_bpf.h \ |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 69 | ) |
| 70 | |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 71 | BPFTOOL_PATHS=( "${!PATH_MAP[@]}" ) |
| 72 | BPFTOOL_VIEW_PATHS=( "${PATH_MAP[@]}" ) |
Quentin Monnet | a1c7c11 | 2021-11-12 01:00:33 +0000 | [diff] [blame] | 73 | BPFTOOL_VIEW_EXCLUDE_REGEX='^(docs/\.gitignore|src/Makefile\.(feature|include))$' |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 74 | LINUX_VIEW_EXCLUDE_REGEX='^$' |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 75 | |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 76 | # Deal with tools/bpf/bpftool first, because once we've mkdir-ed src/, command |
| 77 | # "git mv" doesn't move bpftool _as_ src but _into_ src/. |
| 78 | BPFTOOL_TREE_FILTER="mkdir __bpftool && "$'\\\n' |
| 79 | BPFTOOL_TREE_FILTER+="git mv -kf ${BPFTOOL_SRC_DIR} __bpftool/${PATH_MAP[${BPFTOOL_SRC_DIR}]} && "$'\\\n' |
| 80 | |
| 81 | # Extract bash-completion and Documentation from src/. |
| 82 | BPFTOOL_TREE_FILTER+="git mv -kf __bpftool/src/bash-completion __bpftool/bash-completion && "$'\\\n' |
| 83 | BPFTOOL_TREE_FILTER+="git mv -kf __bpftool/src/Documentation __bpftool/docs && "$'\\\n' |
| 84 | |
Quentin Monnet | 6632092 | 2022-09-28 11:11:37 +0100 | [diff] [blame] | 85 | BPFTOOL_TREE_FILTER+="mkdir -p __bpftool/include/tools __bpftool/include/uapi/asm-generic __bpftool/include/uapi/linux/tc_act __bpftool/src/kernel/bpf && "$'\\\n' |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 86 | for p in "${!PATH_MAP[@]}"; do |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 87 | case ${p} in |
| 88 | ${BPFTOOL_SRC_DIR}*) |
| 89 | continue;; |
| 90 | esac |
| 91 | BPFTOOL_TREE_FILTER+="git mv -kf ${p} __bpftool/${PATH_MAP[${p}]} && "$'\\\n' |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 92 | done |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 93 | BPFTOOL_TREE_FILTER+="true >/dev/null" |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 94 | |
| 95 | cd_to() |
| 96 | { |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 97 | cd "${WORKDIR}" && cd "$1" |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | # Output brief single-line commit description |
| 101 | # $1 - commit ref |
| 102 | commit_desc() |
| 103 | { |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 104 | git log -n1 --pretty='%h ("%s")' "$1" |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | # Create commit single-line signature, which consists of: |
| 108 | # - full commit subject |
| 109 | # - author date in ISO8601 format |
| 110 | # - full commit body with newlines replaced with vertical bars (|) |
| 111 | # - shortstat appended at the end |
| 112 | # The idea is that this single-line signature is good enough to make final |
| 113 | # decision about whether two commits are the same, across different repos. |
| 114 | # $1 - commit ref |
| 115 | # $2 - paths filter |
| 116 | commit_signature() |
| 117 | { |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 118 | # shellcheck disable=SC2086 |
| 119 | git show --pretty='("%s")|%aI|%b' --shortstat "$1" -- ${2-.} | tr '\n' '|' |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 122 | # Cherry-pick commits touching bpftool-related files |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 123 | # $1 - baseline_tag |
| 124 | # $2 - tip_tag |
| 125 | cherry_pick_commits() |
| 126 | { |
| 127 | local manual_mode=${MANUAL_MODE:-0} |
| 128 | local baseline_tag=$1 |
| 129 | local tip_tag=$2 |
| 130 | local new_commits |
| 131 | local signature |
| 132 | local should_skip |
| 133 | local synced_cnt |
| 134 | local manual_check |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 135 | local bpftool_conflict_cnt |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 136 | local desc |
| 137 | |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 138 | # shellcheck disable=SC2068 |
| 139 | new_commits=$(git rev-list --no-merges --topo-order --reverse "${baseline_tag}".."${tip_tag}" ${BPFTOOL_PATHS[@]}) |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 140 | for new_commit in ${new_commits}; do |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 141 | if [[ "${baseline_tag}" == "${BPF_BASELINE_TAG}" ]]; then |
| 142 | if git merge-base --is-ancestor "${new_commit}" "${BASELINE_COMMIT}"; then |
| 143 | echo "Commit ${new_commit::12} from bpf is already in bpf-next branch, skipping." |
| 144 | continue |
| 145 | fi |
| 146 | fi |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 147 | desc="$(commit_desc "${new_commit}")" |
| 148 | signature="$(commit_signature "${new_commit}" "${BPFTOOL_PATHS[*]}")" |
| 149 | # shellcheck disable=SC2126 |
| 150 | synced_cnt=$(grep -F "${signature}" "${TMP_DIR}"/bpftool_commits.txt | wc -l) |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 151 | manual_check=0 |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 152 | if (("${synced_cnt}" > 0)); then |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 153 | # commit with the same subject is already in bpftool, but it's |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 154 | # not 100% the same commit, so check with user |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 155 | echo "Commit '${desc}' is synced into bpftool as:" |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 156 | grep -F "${signature}" "${TMP_DIR}"/bpftool_commits.txt | \ |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 157 | cut -d'|' -f1 | sed -e 's/^/- /' |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 158 | if (("${manual_mode}" != 1 && "${synced_cnt}" == 1)); then |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 159 | echo "Skipping '${desc}' due to unique match..." |
| 160 | continue |
| 161 | fi |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 162 | if (("${synced_cnt}" > 1)); then |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 163 | echo "'${desc} matches multiple commits, please, double-check!" |
| 164 | manual_check=1 |
| 165 | fi |
| 166 | fi |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 167 | if (("${manual_mode}" == 1 || "${manual_check}" == 1)); then |
| 168 | read -rp "Do you want to skip '${desc}'? [y/N]: " should_skip |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 169 | case "${should_skip}" in |
| 170 | "y" | "Y") |
| 171 | echo "Skipping '${desc}'..." |
| 172 | continue |
| 173 | ;; |
| 174 | esac |
| 175 | fi |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 176 | # commit hasn't been synced into bpftool yet |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 177 | echo "Picking '${desc}'..." |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 178 | if ! git cherry-pick "${new_commit}" &>/dev/null; then |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 179 | echo "Warning! Cherry-picking '${desc} failed, checking if it's non-bpftool files causing problems..." |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 180 | # shellcheck disable=SC2068 |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 181 | bpftool_conflict_cnt=$(git diff --name-only --diff-filter=U -- ${BPFTOOL_PATHS[@]} | wc -l) |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 182 | conflict_cnt=$(git diff --name-only | wc -l) |
| 183 | prompt_resolution=1 |
| 184 | |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 185 | if (("${bpftool_conflict_cnt}" == 0)); then |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 186 | echo "Looks like only non-bpftool files have conflicts, ignoring..." |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 187 | if (("${conflict_cnt}" == 0)); then |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 188 | echo "Empty cherry-pick, skipping it..." |
| 189 | git cherry-pick --abort |
| 190 | continue |
| 191 | fi |
| 192 | |
| 193 | git add . |
| 194 | # GIT_EDITOR=true to avoid editor popping up to edit commit message |
| 195 | if ! GIT_EDITOR=true git cherry-pick --continue &>/dev/null; then |
| 196 | echo "Error! That still failed! Please resolve manually." |
| 197 | else |
| 198 | echo "Success! All cherry-pick conflicts were resolved for '${desc}'!" |
| 199 | prompt_resolution=0 |
| 200 | fi |
| 201 | fi |
| 202 | |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 203 | if (("${prompt_resolution}" == 1)); then |
| 204 | read -rp "Error! Cherry-picking '${desc}' failed, please fix manually and press <return> to proceed..." |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 205 | fi |
| 206 | fi |
| 207 | # Append signature of just cherry-picked commit to avoid |
| 208 | # potentially cherry-picking the same commit twice later when |
| 209 | # processing bpf tree commits. At this point we don't know yet |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 210 | # the final commit sha in bpftool repo, so we record Linux SHA |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 211 | # instead as LINUX_<sha>. |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 212 | echo "LINUX_$(git log --pretty='%h' -n1) ${signature}" >> "${TMP_DIR}"/bpftool_commits.txt |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 213 | done |
| 214 | } |
| 215 | |
| 216 | cleanup() |
| 217 | { |
| 218 | echo "Cleaning up..." |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 219 | rm -r -- "${TMP_DIR}" |
| 220 | cd_to "${LINUX_REPO}" |
| 221 | git checkout "${TIP_SYM_REF}" |
| 222 | git branch -D "${BASELINE_TAG}" "${TIP_TAG}" "${BPF_BASELINE_TAG}" "${BPF_TIP_TAG}" \ |
| 223 | "${SQUASH_BASE_TAG}" "${SQUASH_TIP_TAG}" || true |
| 224 | # shellcheck disable=SC2015 |
| 225 | git show-ref --verify --quiet refs/heads/"${VIEW_TAG}" && \ |
| 226 | git branch -D "${VIEW_TAG}" || true |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 227 | |
| 228 | cd_to . |
| 229 | echo "DONE." |
| 230 | } |
| 231 | |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 232 | cd_to "${BPFTOOL_REPO}" |
| 233 | BPFTOOL_SYNC_TAG="bpftool-sync-${SUFFIX}" |
| 234 | git checkout -b "${BPFTOOL_SYNC_TAG}" |
Quentin Monnet | 9ba94fd | 2022-01-16 00:04:19 +0000 | [diff] [blame] | 235 | |
| 236 | # Update libbpf |
| 237 | if [[ "${SKIP_LIBBPF_UPDATE:-0}" -ne 1 ]]; then |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 238 | cd_to "${BPFTOOL_REPO}"/libbpf |
Quentin Monnet | 9ba94fd | 2022-01-16 00:04:19 +0000 | [diff] [blame] | 239 | git pull origin master |
| 240 | LIBBPF_VERSION=$(grep -oE '^LIBBPF_([0-9.]+)' src/libbpf.map | sort -rV | head -n1 | cut -d'_' -f2) |
| 241 | LIBBPF_COMMIT=$(git rev-parse HEAD) |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 242 | cd_to "${BPFTOOL_REPO}" |
Quentin Monnet | 9ba94fd | 2022-01-16 00:04:19 +0000 | [diff] [blame] | 243 | if [[ -n "$(git status --porcelain --untracked-files=no)" ]]; then |
| 244 | git add libbpf |
Quentin Monnet | d10e1e1 | 2022-11-16 17:43:38 +0000 | [diff] [blame] | 245 | git commit --signoff -m 'sync: Update libbpf submodule' \ |
Quentin Monnet | 9ba94fd | 2022-01-16 00:04:19 +0000 | [diff] [blame] | 246 | -m "\ |
| 247 | Pull latest libbpf from mirror. |
| 248 | Libbpf version: ${LIBBPF_VERSION} |
| 249 | Libbpf commit: ${LIBBPF_COMMIT}" \ |
| 250 | -- libbpf |
| 251 | fi |
| 252 | fi |
| 253 | |
| 254 | # Use libbpf's new checkpoints as tips |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 255 | TIP_COMMIT=${BPF_NEXT_TIP_COMMIT:-$(cat "${BPFTOOL_REPO}"/libbpf/CHECKPOINT-COMMIT)} |
| 256 | BPF_TIP_COMMIT=${BPF_TIP_COMMIT:-$(cat "${BPFTOOL_REPO}"/libbpf/BPF-CHECKPOINT-COMMIT)} |
Quentin Monnet | 9ba94fd | 2022-01-16 00:04:19 +0000 | [diff] [blame] | 257 | if [ -z "${TIP_COMMIT}" ] || [ -z "${BPF_TIP_COMMIT}" ]; then |
| 258 | echo "Error: bpf or bpf-next tip commits are not provided" |
| 259 | usage |
| 260 | fi |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 261 | |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 262 | cd_to "${BPFTOOL_REPO}" |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 263 | GITHUB_ABS_DIR=$(pwd) |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 264 | echo "Dumping existing bpftool commit signatures..." |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 265 | for h in $(git log --pretty='%h' -n500); do |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 266 | echo "$h" "$(commit_signature "$h")" >> "${TMP_DIR}"/bpftool_commits.txt |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 267 | done |
| 268 | |
| 269 | # Use current kernel repo HEAD as a source of patches |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 270 | cd_to "${LINUX_REPO}" |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 271 | LINUX_ABS_DIR=$(pwd) |
| 272 | TIP_SYM_REF=$(git symbolic-ref -q --short HEAD || git rev-parse HEAD) |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 273 | BASELINE_TAG="bpftool-baseline-${SUFFIX}" |
| 274 | TIP_TAG="bpftool-tip-${SUFFIX}" |
| 275 | BPF_BASELINE_TAG="bpftool-bpf-baseline-${SUFFIX}" |
| 276 | BPF_TIP_TAG="bpftool-bpf-tip-${SUFFIX}" |
| 277 | VIEW_TAG="bpftool-view-${SUFFIX}" |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 278 | |
| 279 | # Squash state of kernel repo at baseline into single commit |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 280 | SQUASH_BASE_TAG="bpftool-squash-base-${SUFFIX}" |
| 281 | SQUASH_TIP_TAG="bpftool-squash-tip-${SUFFIX}" |
| 282 | SQUASH_COMMIT=$(git commit-tree "${BASELINE_COMMIT}^{tree}" -m "BASELINE SQUASH ${BASELINE_COMMIT}") |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 283 | |
| 284 | echo "WORKDIR: ${WORKDIR}" |
| 285 | echo "LINUX REPO: ${LINUX_REPO}" |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 286 | echo "BPFTOOL REPO: ${BPFTOOL_REPO}" |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 287 | echo "TEMP DIR: ${TMP_DIR}" |
| 288 | echo "SUFFIX: ${SUFFIX}" |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 289 | echo "BASE COMMIT: '$(commit_desc "${BASELINE_COMMIT}")'" |
| 290 | echo "TIP COMMIT: '$(commit_desc "${TIP_COMMIT}")'" |
| 291 | echo "BPF BASE COMMIT: '$(commit_desc "${BPF_BASELINE_COMMIT}")'" |
| 292 | echo "BPF TIP COMMIT: '$(commit_desc "${BPF_TIP_COMMIT}")'" |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 293 | echo "SQUASH COMMIT: ${SQUASH_COMMIT}" |
| 294 | echo "BASELINE TAG: ${BASELINE_TAG}" |
| 295 | echo "TIP TAG: ${TIP_TAG}" |
| 296 | echo "BPF BASELINE TAG: ${BPF_BASELINE_TAG}" |
| 297 | echo "BPF TIP TAG: ${BPF_TIP_TAG}" |
| 298 | echo "SQUASH BASE TAG: ${SQUASH_BASE_TAG}" |
| 299 | echo "SQUASH TIP TAG: ${SQUASH_TIP_TAG}" |
| 300 | echo "VIEW TAG: ${VIEW_TAG}" |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 301 | echo "BPFTOOL SYNC TAG: ${BPFTOOL_SYNC_TAG}" |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 302 | echo "PATCHES: ${TMP_DIR}/patches" |
| 303 | |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 304 | git branch "${BASELINE_TAG}" "${BASELINE_COMMIT}" |
| 305 | git branch "${TIP_TAG}" "${TIP_COMMIT}" |
| 306 | git branch "${BPF_BASELINE_TAG}" "${BPF_BASELINE_COMMIT}" |
| 307 | git branch "${BPF_TIP_TAG}" "${BPF_TIP_COMMIT}" |
| 308 | git branch "${SQUASH_BASE_TAG}" "${SQUASH_COMMIT}" |
| 309 | git checkout -b "${SQUASH_TIP_TAG}" "${SQUASH_COMMIT}" |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 310 | |
| 311 | # Cherry-pick new commits onto squashed baseline commit |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 312 | echo "Cherry-pick for bpf-next..." |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 313 | cherry_pick_commits "${BASELINE_TAG}" "${TIP_TAG}" |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 314 | echo "Cherry-pick for bpf..." |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 315 | cherry_pick_commits "${BPF_BASELINE_TAG}" "${BPF_TIP_TAG}" |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 316 | |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 317 | # Move all bpftool files into __bpftool directory. |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 318 | FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch --prune-empty -f --tree-filter "${BPFTOOL_TREE_FILTER}" "${SQUASH_TIP_TAG}" "${SQUASH_BASE_TAG}" |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 319 | # Make __bpftool a new root directory |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 320 | FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch --prune-empty -f --subdirectory-filter __bpftool "${SQUASH_TIP_TAG}" "${SQUASH_BASE_TAG}" |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 321 | |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 322 | # If there are no new commits with bpftool-related changes, bail out |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 323 | COMMIT_CNT=$(git rev-list --count "${SQUASH_BASE_TAG}".."${SQUASH_TIP_TAG}") |
| 324 | if (("${COMMIT_CNT}" <= 0)); then |
| 325 | echo "No new changes to apply, we are done!" |
| 326 | cleanup |
| 327 | exit 2 |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 328 | fi |
| 329 | |
| 330 | # Exclude baseline commit and generate nice cover letter with summary |
Quentin Monnet | d10e1e1 | 2022-11-16 17:43:38 +0000 | [diff] [blame] | 331 | git format-patch --no-signature "${SQUASH_BASE_TAG}".."${SQUASH_TIP_TAG}" --cover-letter -o "${TMP_DIR}"/patches |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 332 | |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 333 | # Now is time to re-apply bpftool-related linux patches to bpftool repo |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 334 | cd_to "${BPFTOOL_REPO}" |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 335 | |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 336 | # shellcheck disable=SC2012 |
| 337 | for patch in $(ls -1 "${TMP_DIR}"/patches | tail -n +2); do |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 338 | if ! git am --3way --committer-date-is-author-date "${TMP_DIR}/patches/${patch}"; then |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 339 | read -rp "Applying ${TMP_DIR}/patches/${patch} failed, please resolve manually and press <return> to proceed..." |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 340 | fi |
| 341 | done |
| 342 | |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 343 | # Use generated cover-letter as a template for "sync commit" with |
| 344 | # baseline and checkpoint commits from kernel repo (and leave summary |
| 345 | # from cover letter intact, of course) |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 346 | echo "${TIP_COMMIT}" > CHECKPOINT-COMMIT && \ |
| 347 | echo "${BPF_TIP_COMMIT}" > BPF-CHECKPOINT-COMMIT && \ |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 348 | git add CHECKPOINT-COMMIT && \ |
| 349 | git add BPF-CHECKPOINT-COMMIT && \ |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 350 | awk '/\*\*\* BLURB HERE \*\*\*/ {p=1} p' "${TMP_DIR}"/patches/0000-cover-letter.patch | \ |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 351 | sed "s/\*\*\* BLURB HERE \*\*\*/\ |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 352 | sync: Pull latest bpftool changes from kernel\n\ |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 353 | \n\ |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 354 | Syncing latest bpftool commits from kernel repository.\n\ |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 355 | Baseline bpf-next commit: ${BASELINE_COMMIT}\n\ |
| 356 | Checkpoint bpf-next commit: ${TIP_COMMIT}\n\ |
| 357 | Baseline bpf commit: ${BPF_BASELINE_COMMIT}\n\ |
Quentin Monnet | 4dfcd14 | 2022-02-16 15:22:20 +0000 | [diff] [blame] | 358 | Checkpoint bpf commit: ${BPF_TIP_COMMIT}/" | \ |
Quentin Monnet | d10e1e1 | 2022-11-16 17:43:38 +0000 | [diff] [blame] | 359 | git commit --signoff --file=- |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 360 | |
| 361 | echo "SUCCESS! ${COMMIT_CNT} commits synced." |
| 362 | |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 363 | echo "Verifying Linux's and Github's bpftool state" |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 364 | |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 365 | cd_to "${LINUX_REPO}" |
| 366 | git checkout -b "${VIEW_TAG}" "${TIP_COMMIT}" |
| 367 | FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch -f --tree-filter "${BPFTOOL_TREE_FILTER}" "${VIEW_TAG}"^.."${VIEW_TAG}" |
| 368 | FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch -f --subdirectory-filter __bpftool "${VIEW_TAG}"^.."${VIEW_TAG}" |
| 369 | # shellcheck disable=SC2068 |
| 370 | git ls-files -- ${BPFTOOL_VIEW_PATHS[@]} | grep -v -E "${LINUX_VIEW_EXCLUDE_REGEX}" > "${TMP_DIR}"/linux-view.ls |
Quentin Monnet | 7ee73da | 2022-01-27 22:05:28 +0000 | [diff] [blame] | 371 | # Before we compare each file, try to apply to the mirror a patch containing the |
| 372 | # expected differences between the two repositories; this is to avoid checking |
| 373 | # "known" differences visually and taking the risk of missing a new, relevant |
| 374 | # differences. |
| 375 | echo "Patching to account for expected differences..." |
| 376 | patch -d "${LINUX_ABS_DIR}" -p0 -f --reject-file=- --no-backup-if-mismatch < "${GITHUB_ABS_DIR}/scripts/sync-kernel-expected-diff.patch" || true |
| 377 | git add -u |
| 378 | git commit -m 'tmp: apply expected differences to compare github/kernel repos' || true |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 379 | |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 380 | cd_to "${BPFTOOL_REPO}" |
| 381 | # shellcheck disable=SC2068 |
| 382 | git ls-files -- ${BPFTOOL_VIEW_PATHS[@]} | grep -v -E "${BPFTOOL_VIEW_EXCLUDE_REGEX}" > "${TMP_DIR}"/github-view.ls |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 383 | |
| 384 | echo "Comparing list of files..." |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 385 | diff -u "${TMP_DIR}"/linux-view.ls "${TMP_DIR}"/github-view.ls |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 386 | echo "Comparing file contents..." |
| 387 | CONSISTENT=1 |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 388 | while IFS= read -r F; do |
Quentin Monnet | 164585e | 2021-11-12 00:57:35 +0000 | [diff] [blame] | 389 | if ! diff -u --color "${LINUX_ABS_DIR}/${F}" "${GITHUB_ABS_DIR}/${F}"; then |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 390 | echo "${LINUX_ABS_DIR}/${F} and ${GITHUB_ABS_DIR}/${F} are different!" |
| 391 | CONSISTENT=0 |
| 392 | fi |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 393 | done < "${TMP_DIR}"/linux-view.ls |
Quentin Monnet | 7ee73da | 2022-01-27 22:05:28 +0000 | [diff] [blame] | 394 | echo "" |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 395 | if (("${CONSISTENT}" == 1)); then |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 396 | echo "Great! Content is identical!" |
| 397 | else |
| 398 | ignore_inconsistency=n |
| 399 | echo "Unfortunately, there are some inconsistencies, please double check." |
Quentin Monnet | 7ee73da | 2022-01-27 22:05:28 +0000 | [diff] [blame] | 400 | echo "Some of them may come from patches in bpf tree but absent from bpf-next." |
| 401 | echo "Note: I applied scripts/sync-kernel-expected-diff.patch before checking," |
| 402 | echo "to account for expected changes. If this patch needs an update," |
| 403 | echo "you can do it now with:" |
| 404 | echo "------" |
| 405 | echo " (cd \"${LINUX_ABS_DIR}\" && git -c advice.detachedHead=false checkout HEAD~)" |
| 406 | echo " for f in \$(cat \"${TMP_DIR}/linux-view.ls\"); do" |
| 407 | echo " diff -u --label \"\${f}\" --label \"\${f}\" \\" |
| 408 | echo " \"${LINUX_ABS_DIR}/\${f}\" \\" |
| 409 | echo " \"${GITHUB_ABS_DIR}/\${f}\"" |
| 410 | echo " done > \"${GITHUB_ABS_DIR}/scripts/sync-kernel-expected-diff.patch\"" |
| 411 | echo "------" |
Quentin Monnet | 8e1d726 | 2022-09-28 18:39:14 +0100 | [diff] [blame] | 412 | read -rp "Does everything look good? [y/N]: " ignore_inconsistency |
Quentin Monnet | 1e2278e | 2021-11-12 00:56:33 +0000 | [diff] [blame] | 413 | case "${ignore_inconsistency}" in |
| 414 | "y" | "Y") |
| 415 | echo "Ok, proceeding..." |
| 416 | ;; |
| 417 | *) |
| 418 | echo "Oops, exiting with error..." |
| 419 | exit 4 |
| 420 | esac |
| 421 | fi |
| 422 | |
| 423 | cleanup |