Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) #!/bin/bash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) # SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) # Copyright (c) 2018 Jesper Dangaard Brouer, Red Hat Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) # Bash-shell example on using iproute2 tools 'tc' and 'ip' to load
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) # eBPF programs, both for XDP and clsbpf.  Shell script function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) # wrappers and even long options parsing is illustrated, for ease of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) # use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) # Related to sample/bpf/xdp2skb_meta_kern.c, which contains BPF-progs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) # that need to collaborate between XDP and TC hooks.  Thus, it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) # convenient that the same tool load both programs that need to work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) # together.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) BPF_FILE=xdp2skb_meta_kern.o
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) DIR=$(dirname $0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) [ -z "$TC" ] && TC=tc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) [ -z "$IP" ] && IP=ip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) function usage() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)     echo ""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)     echo "Usage: $0 [-vfh] --dev ethX"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)     echo "  -d | --dev     :             Network device (required)"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)     echo "  --flush        :             Cleanup flush TC and XDP progs"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)     echo "  --list         : (\$LIST)     List TC and XDP progs"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)     echo "  -v | --verbose : (\$VERBOSE)  Verbose"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)     echo "  --dry-run      : (\$DRYRUN)   Dry-run only (echo commands)"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)     echo ""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) ## -- General shell logging cmds --
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) function err() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)     local exitcode=$1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)     shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)     echo "ERROR: $@" >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)     exit $exitcode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) function info() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)     if [[ -n "$VERBOSE" ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	echo "# $@"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)     fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) ## -- Helper function calls --
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) # Wrapper call for TC and IP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) # - Will display the offending command on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) function _call_cmd() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)     local cmd="$1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)     local allow_fail="$2"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)     shift 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)     if [[ -n "$VERBOSE" ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	echo "$cmd $@"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)     fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)     if [[ -n "$DRYRUN" ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)     fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)     $cmd "$@"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)     local status=$?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)     if (( $status != 0 )); then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if [[ "$allow_fail" == "" ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	    err 2 "Exec error($status) occurred cmd: \"$cmd $@\""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)     fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) function call_tc() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)     _call_cmd "$TC" "" "$@"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) function call_tc_allow_fail() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)     _call_cmd "$TC" "allow_fail" "$@"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) function call_ip() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)     _call_cmd "$IP" "" "$@"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) ##  --- Parse command line arguments / parameters ---
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) # Using external program "getopt" to get --long-options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) OPTIONS=$(getopt -o vfhd: \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)     --long verbose,flush,help,list,dev:,dry-run -- "$@")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) if (( $? != 0 )); then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)     err 4 "Error calling getopt"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) eval set -- "$OPTIONS"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) unset DEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) unset FLUSH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) while true; do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)     case "$1" in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	-d | --dev ) # device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	    DEV=$2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	    info "Device set to: DEV=$DEV" >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	    shift 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	    ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	-v | --verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	    VERBOSE=yes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	    # info "Verbose mode: VERBOSE=$VERBOSE" >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	    shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	    ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	--dry-run )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	    DRYRUN=yes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	    VERBOSE=yes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	    info "Dry-run mode: enable VERBOSE and don't call TC+IP" >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	    shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)             ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	-f | --flush )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	    FLUSH=yes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	    shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	    ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	--list )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	    LIST=yes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	    shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	    ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	-- )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	    shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	    break
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	    ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	-h | --help )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	    usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	    exit 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	    ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	* )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	    shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	    break
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	    ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)     esac
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) FILE="$DIR/$BPF_FILE"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if [[ ! -e $FILE ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)     err 3 "Missing BPF object file ($FILE)"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if [[ -z $DEV ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)     usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)     err 2 "Please specify network device -- required option --dev"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) ## -- Function calls --
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) function list_tc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)     local device="$1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)     shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)     info "Listing current TC ingress rules"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)     call_tc filter show dev $device ingress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) function list_xdp()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)     local device="$1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)     shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)     info "Listing current XDP device($device) setting"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)     call_ip link show dev $device | grep --color=auto xdp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) function flush_tc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)     local device="$1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)     shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)     info "Flush TC on device: $device"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)     call_tc_allow_fail filter del dev $device ingress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)     call_tc_allow_fail qdisc del dev $device clsact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) function flush_xdp()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)     local device="$1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)     shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)     info "Flush XDP on device: $device"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)     call_ip link set dev $device xdp off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) function attach_tc_mark()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)     local device="$1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)     local file="$2"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)     local prog="tc_mark"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)     shift 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)     # Re-attach clsact to clear/flush existing role
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)     call_tc_allow_fail qdisc del dev $device clsact 2> /dev/null
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)     call_tc            qdisc add dev $device clsact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)     # Attach BPF prog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)     call_tc filter add dev $device ingress \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	    prio 1 handle 1 bpf da obj $file sec $prog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) function attach_xdp_mark()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)     local device="$1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)     local file="$2"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)     local prog="xdp_mark"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)     shift 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)     # Remove XDP prog in-case it's already loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)     # TODO: Need ip-link option to override/replace existing XDP prog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)     flush_xdp $device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)     # Attach XDP/BPF prog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)     call_ip link set dev $device xdp obj $file sec $prog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if [[ -n $FLUSH ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)     flush_tc  $DEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)     flush_xdp $DEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)     exit 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if [[ -n $LIST ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)     list_tc  $DEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)     list_xdp $DEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)     exit 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) attach_tc_mark  $DEV $FILE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) attach_xdp_mark $DEV $FILE