github.com/fafucoder/cilium@v1.6.11/bpf/run_probes.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright 2016-2017 Authors of Cilium
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  set -x
    18  set -e
    19  set -u
    20  
    21  LIB=$1
    22  RUNDIR=$2
    23  
    24  PROBE_DIR=$(mktemp -d)
    25  FEATURE_FILE="$RUNDIR/globals/bpf_features.h"
    26  INFO_FILE="$RUNDIR/bpf_features.log"
    27  WARNING_FILE="$RUNDIR/bpf_requirements.log"
    28  
    29  function cleanup {
    30  	if [ ! -z "$PROBE_DIR" ]; then
    31  		rm -rf "$PROBE_DIR"
    32  	fi
    33  }
    34  
    35  trap cleanup EXIT
    36  
    37  function probe_kernel_config()
    38  {
    39      # BPF Kernel params verifier.
    40      local KCONFIG=""
    41      local RESULT=0
    42      # Coreos kernel config is on /proc/config (module configs).
    43      # Other distros in /boot/config-*
    44      local config_locations=("/proc/config" "/proc/config.gz"
    45          "/boot/config-$(uname -r)")
    46      local REQ_PARAMS=(
    47          "CONFIG_BPF=y" "CONFIG_BPF_SYSCALL=y" "CONFIG_NET_SCH_INGRESS=[m|y]"
    48          "CONFIG_NET_CLS_BPF=[m|y]" "CONFIG_NET_CLS_ACT=y" "CONFIG_BPF_JIT=y"
    49          "CONFIG_HAVE_EBPF_JIT=y")
    50      local OPT_PARAMS=(
    51          "CONFIG_CGROUP_BPF=y" "CONFIG_LWTUNNEL_BPF=y" "CONFIG_BPF_EVENTS=y")
    52  
    53      for config in "${config_locations[@]}"
    54      do
    55          if [[ -f "$config" ]]; then
    56              KCONFIG=$config
    57              break
    58          fi
    59      done
    60  
    61      if [[ -z "$KCONFIG" ]]; then
    62          echo "BPF/probes: Kernel config not found." >> $INFO_FILE
    63          return
    64      fi
    65  
    66      for key in "${OPT_PARAMS[@]}"
    67      do
    68          zgrep -E "${key}" $KCONFIG > /dev/null || {
    69              echo "BPF/probes: ${key} is not in kernel configuration" >> $INFO_FILE
    70              }
    71      done
    72  
    73      for key in "${REQ_PARAMS[@]}"
    74      do
    75          zgrep -E "${key}" $KCONFIG > /dev/null || {
    76              RESULT=1;
    77              echo "BPF/probes: ${key} is not in kernel configuration" >> $WARNING_FILE
    78              }
    79      done
    80  
    81      if [[ "$RESULT" -gt 0 ]]; then
    82          echo "BPF/probes: Missing kernel configuration" >> $WARNING_FILE
    83      fi
    84  }
    85  
    86  
    87  # High level probes that require to invoke tc.
    88  function probe_run_tc()
    89  {
    90  	PROBE="${LIB}/probes/$1"
    91  	OUT="$PROBE_DIR/${1}.o"
    92  	FEATURE=$2
    93  	tc qdisc del dev $DEV clsact 2> /dev/null
    94  
    95  	PROBE_OPTS="-D__NR_CPUS__=$(nproc) -O2 -target bpf -I$DIR -I. -I$LIB/include -Wall -Wno-address-of-packed-member -Wno-unknown-warning-option"
    96  
    97  	clang $PROBE_OPTS -c "$PROBE" -o "$OUT" &&
    98  	tc qdisc add dev $DEV clsact &&
    99  	tc filter add dev $DEV ingress bpf da obj $OUT sec probe &&
   100  	echo "#define $FEATURE" >> "$FEATURE_FILE"
   101  }
   102  
   103  function prep_probe()
   104  {
   105  	OUT_FILE=$1
   106  	# Various extensions for textual replacement go here.
   107  	awk -F":" '/insn-repeat/ { for(i = 0;i < $2; i++) print }; { print }' "$OUT_FILE" > \
   108  	      "$OUT/tmp.t" && mv "$OUT/tmp.t" "$OUT_FILE"
   109  }
   110  
   111  # Low level probes that only check verifier.
   112  function probe_run_ll()
   113  {
   114  	PROBE_BASE="${LIB}/probes"
   115  	OUT="$PROBE_DIR"
   116  	LIB_INCLUDE="${LIB}/include"
   117  	PROBE_OPTS="-O2 -I$OUT -I$PROBE_BASE -I$LIB_INCLUDE -Wall"
   118  
   119  	for PROBE in "${PROBE_BASE}"/*.t
   120  	do
   121  		OUT_BIN=`basename "$PROBE"`
   122  		cp "$PROBE" "$OUT/raw_probe.t"
   123  		prep_probe "$OUT/raw_probe.t"
   124  		clang $PROBE_OPTS "$PROBE_BASE/raw_main.c" -o "$OUT/$OUT_BIN" &&
   125  		"$OUT/$OUT_BIN" 1>> "$FEATURE_FILE" 2>> "$INFO_FILE"
   126  	done
   127  }
   128  
   129  for file in $INFO_FILE $WARNING_FILE
   130  do
   131  	rm -f "$file"
   132  done
   133  
   134  echo "#ifndef BPF_FEATURES_H_"  > "$FEATURE_FILE"
   135  echo "#define BPF_FEATURES_H_" >> "$FEATURE_FILE"
   136  echo "" >> "$FEATURE_FILE"
   137  
   138  #probe_run_tc "skb_change_tail.c" "HAVE_SKB_CHANGE_TAIL"
   139  probe_kernel_config
   140  probe_run_ll
   141  
   142  echo "#endif /* BPF_FEATURES_H_ */" >> "$FEATURE_FILE"
   143  
   144  for file in $INFO_FILE $WARNING_FILE
   145  do
   146  	if [ ! -s "$file" ]; then
   147  		rm -f "$file"
   148  	fi
   149  done