From: Matt Corallo Date: Thu, 9 Dec 2021 00:44:57 +0000 (+0000) Subject: Make dropcount not stupid slow X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=fb716f036f2dcc480b50e094351c908720389188;p=flowspec-xdp Make dropcount not stupid slow --- diff --git a/dropcount.sh b/dropcount.sh index 5f15b5e..6278955 100755 --- a/dropcount.sh +++ b/dropcount.sh @@ -1,47 +1,41 @@ #!/bin/bash -function PRINTCNT() { -if [ "$1" != "" ]; then - if [ "$1" = "0" ]; then - echo -e "$2\t$3\tInvalid packet length" - elif [ "$1" = "1" ]; then - echo -e "$2\t$3\tInvalid VLAN tag" - elif [ "$1" = "2" ]; then - echo -e "$2\t$3\tInvalid/rejected IHL IPv4 field" - elif [ "$1" = "3" ]; then - echo -e "$2\t$3\tRejected IPv6 fragments" - else - echo -en "$2\t$3\t" - cat "$(dirname ${BASH_SOURCE[0]})/installed-rules.txt" | head -n $(( $1 - 3 )) | tail -n1 - fi -fi -CNT=0 -} MAP_CONTENTS="$(bpftool map show | grep drop_cnt_map | awk '{ print $1 }' | tr -d ':' | while read IF; do - bpftool map dump id "$IF" | grep "bytes\|packets\|key" | grep -v '\(bytes\|packets\)": 0\(,\)*$' + bpftool map dump id "$IF" | grep "bytes\|packets\|key" | grep -v '\(bytes\|packets\)": 0\(,\)*$' | tr -d ',' done)" echo "$MAP_CONTENTS" | { declare -a BYTES declare -a PACKETS + TOTAL_PACKETS=0 + TOTAL_BYTES=0 KEY="" while read LINE; do case "$LINE" in *"key"*) - KEY=$(echo "$LINE" | awk '{ print $2 }' | tr -d ',') + KEY=${LINE:7} if [ "${BYTES["${KEY}"]}" = "" ]; then BYTES["${KEY}"]=0 PACKETS["${KEY}"]=0 fi ;; *"bytes"*) - BYTES["${KEY}"]=$(( ${BYTES["$KEY"]} + $(echo "$LINE" | awk '{ print $2 }' | tr -d ',') )) + BYTES["${KEY}"]=$(( ${BYTES["$KEY"]} + ${LINE:9} )) + TOTAL_BYTES=$(( $TOTAL_BYTES + ${LINE:9} )) ;; *"packets"*) - PACKETS["$KEY"]=$(( ${PACKETS["$KEY"]} + $(echo "$LINE" | awk '{ print $2 }' | tr -d ',') )) + PACKETS["$KEY"]=$(( ${PACKETS["$KEY"]} + ${LINE:11} )) + TOTAL_PACKETS=$(( $TOTAL_PACKETS + ${LINE:11} )) ;; esac done echo -e "pkts\tKBytes\tRule" - for C in "${!BYTES[@]}"; do - PRINTCNT $C "${PACKETS["$C"]}" "$(( ${BYTES["$C"]} / 1000 ))" - done + echo -e "${TOTAL_PACKETS}\t$(( ${TOTAL_BYTES} / 1000 ))\tTotal" + echo -e "${PACKETS[0]}\t$(( ${BYTES[0]} / 1000 ))\tInvalid packet length" + echo -e "${PACKETS[1]}\t$(( ${BYTES[1]} / 1000 ))\tInvalid VLAN tag" + echo -e "${PACKETS[2]}\t$(( ${BYTES[2]} / 1000 ))\tInvalid/rejected IHL IPv4 field" + echo -e "${PACKETS[3]}\t$(( ${BYTES[3]} / 1000 ))\tRejected IPv6 fragments" + C=4 + while read LINE; do + echo -e "${PACKETS["$C"]}\t$(( ${BYTES["$C"]} / 1000 ))\t$LINE" + C=$(( $C + 1 )) + done < "$(dirname ${BASH_SOURCE[0]})/installed-rules.txt" }