]> git.bitcoin.ninja Git - flowspec-xdp/commitdiff
Make dropcount not stupid slow
authorMatt Corallo <git@bluematt.me>
Thu, 9 Dec 2021 00:44:57 +0000 (00:44 +0000)
committerMatt Corallo <git@bluematt.me>
Thu, 9 Dec 2021 01:44:20 +0000 (01:44 +0000)
dropcount.sh

index 5f15b5e6ab087f7e6cea34a2a686934aa0d7388f..6278955bb1e967fb60eeb846647c7632a0cfea8f 100755 (executable)
@@ -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"
 }