221 lines · bash
1#!/bin/bash2#3# SPDX-License-Identifier: GPL-2.04# Copyright (c) 2018 Jesper Dangaard Brouer, Red Hat Inc.5#6# Bash-shell example on using iproute2 tools 'tc' and 'ip' to load7# eBPF programs, both for XDP and clsbpf. Shell script function8# wrappers and even long options parsing is illustrated, for ease of9# use.10#11# Related to sample/bpf/xdp2skb_meta_kern.c, which contains BPF-progs12# that need to collaborate between XDP and TC hooks. Thus, it is13# convenient that the same tool load both programs that need to work14# together.15#16BPF_FILE=xdp2skb_meta_kern.o17DIR=$(dirname $0)18 19[ -z "$TC" ] && TC=tc20[ -z "$IP" ] && IP=ip21 22function usage() {23 echo ""24 echo "Usage: $0 [-vfh] --dev ethX"25 echo " -d | --dev : Network device (required)"26 echo " --flush : Cleanup flush TC and XDP progs"27 echo " --list : (\$LIST) List TC and XDP progs"28 echo " -v | --verbose : (\$VERBOSE) Verbose"29 echo " --dry-run : (\$DRYRUN) Dry-run only (echo commands)"30 echo ""31}32 33## -- General shell logging cmds --34function err() {35 local exitcode=$136 shift37 echo "ERROR: $@" >&238 exit $exitcode39}40 41function info() {42 if [[ -n "$VERBOSE" ]]; then43 echo "# $@"44 fi45}46 47## -- Helper function calls --48 49# Wrapper call for TC and IP50# - Will display the offending command on failure51function _call_cmd() {52 local cmd="$1"53 local allow_fail="$2"54 shift 255 if [[ -n "$VERBOSE" ]]; then56 echo "$cmd $@"57 fi58 if [[ -n "$DRYRUN" ]]; then59 return60 fi61 $cmd "$@"62 local status=$?63 if (( $status != 0 )); then64 if [[ "$allow_fail" == "" ]]; then65 err 2 "Exec error($status) occurred cmd: \"$cmd $@\""66 fi67 fi68}69function call_tc() {70 _call_cmd "$TC" "" "$@"71}72function call_tc_allow_fail() {73 _call_cmd "$TC" "allow_fail" "$@"74}75function call_ip() {76 _call_cmd "$IP" "" "$@"77}78 79## --- Parse command line arguments / parameters ---80# Using external program "getopt" to get --long-options81OPTIONS=$(getopt -o vfhd: \82 --long verbose,flush,help,list,dev:,dry-run -- "$@")83if (( $? != 0 )); then84 err 4 "Error calling getopt"85fi86eval set -- "$OPTIONS"87 88unset DEV89unset FLUSH90while true; do91 case "$1" in92 -d | --dev ) # device93 DEV=$294 info "Device set to: DEV=$DEV" >&295 shift 296 ;;97 -v | --verbose)98 VERBOSE=yes99 # info "Verbose mode: VERBOSE=$VERBOSE" >&2100 shift101 ;;102 --dry-run )103 DRYRUN=yes104 VERBOSE=yes105 info "Dry-run mode: enable VERBOSE and don't call TC+IP" >&2106 shift107 ;;108 -f | --flush )109 FLUSH=yes110 shift111 ;;112 --list )113 LIST=yes114 shift115 ;;116 -- )117 shift118 break119 ;;120 -h | --help )121 usage;122 exit 0123 ;;124 * )125 shift126 break127 ;;128 esac129done130 131FILE="$DIR/$BPF_FILE"132if [[ ! -e $FILE ]]; then133 err 3 "Missing BPF object file ($FILE)"134fi135 136if [[ -z $DEV ]]; then137 usage138 err 2 "Please specify network device -- required option --dev"139fi140 141## -- Function calls --142 143function list_tc()144{145 local device="$1"146 shift147 info "Listing current TC ingress rules"148 call_tc filter show dev $device ingress149}150 151function list_xdp()152{153 local device="$1"154 shift155 info "Listing current XDP device($device) setting"156 call_ip link show dev $device | grep --color=auto xdp157}158 159function flush_tc()160{161 local device="$1"162 shift163 info "Flush TC on device: $device"164 call_tc_allow_fail filter del dev $device ingress165 call_tc_allow_fail qdisc del dev $device clsact166}167 168function flush_xdp()169{170 local device="$1"171 shift172 info "Flush XDP on device: $device"173 call_ip link set dev $device xdp off174}175 176function attach_tc_mark()177{178 local device="$1"179 local file="$2"180 local prog="tc_mark"181 shift 2182 183 # Re-attach clsact to clear/flush existing role184 call_tc_allow_fail qdisc del dev $device clsact 2> /dev/null185 call_tc qdisc add dev $device clsact186 187 # Attach BPF prog188 call_tc filter add dev $device ingress \189 prio 1 handle 1 bpf da obj $file sec $prog190}191 192function attach_xdp_mark()193{194 local device="$1"195 local file="$2"196 local prog="xdp_mark"197 shift 2198 199 # Remove XDP prog in-case it's already loaded200 # TODO: Need ip-link option to override/replace existing XDP prog201 flush_xdp $device202 203 # Attach XDP/BPF prog204 call_ip link set dev $device xdp obj $file sec $prog205}206 207if [[ -n $FLUSH ]]; then208 flush_tc $DEV209 flush_xdp $DEV210 exit 0211fi212 213if [[ -n $LIST ]]; then214 list_tc $DEV215 list_xdp $DEV216 exit 0217fi218 219attach_tc_mark $DEV $FILE220attach_xdp_mark $DEV $FILE221