Skip to content

Commit

Permalink
bpfcountd: adding initial package
Browse files Browse the repository at this point in the history
bpfcountd was created to obtain packet statistics in larger networks
without stressing the cpu resources. bpfcountd will count the amount
of packages and bytes over time (for each defined rule). The rules
are defined using the tcpdump filter syntax (bpf). The collected
data is provided on unix socket in plaintext.

Signed-off-by: Linus Lüssing <[email protected]>
  • Loading branch information
T-X committed Mar 15, 2022
1 parent 31124ac commit cc2cafd
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
34 changes: 34 additions & 0 deletions package/bpfcountd/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=bpfcountd
PKG_SOURCE_DATE:=2020-01-01
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL=https://github.com/lemoer/bpfcountd.git
PKG_SOURCE_VERSION:=9f602a1e8e691749ca233ede06846c7e53623bc5

PKG_BUILD_DEPENDS += libpcap

include $(INCLUDE_DIR)/package.mk

define Package/bpfcountd
SECTION:=net
CATEGORY:=Network
TITLE:=Berkeley Packet Filter Counting Daemon
DEPENDS:= +libpcap
endef

define Package/bpfcountd/description
bpfcountd was created to obtain packet statistics in larger networks
without stressing the cpu resources. bpfcountd will count the amount
of packages and bytes over time (for each defined rule). The rules
are defined using the tcpdump filter syntax (bpf). The collected
data is provided on unix socket in plaintext.
endef

define Package/bpfcountd/install
$(INSTALL_DIR) $(1)/usr/sbin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/bpfcountd $(1)/usr/sbin/
$(CP) ./files/* $(1)/
endef

$(eval $(call BuildPackage,bpfcountd))
2 changes: 2 additions & 0 deletions package/bpfcountd/files/etc/bpfcountd.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
arp;arp
icmp6;icmp6
12 changes: 12 additions & 0 deletions package/bpfcountd/files/etc/config/bpfcountd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
config bpfcountd 'eth0_in'
option ifname 'eth0'
option prefilter 'inbound'
option filterfile '/etc/bpfcountd.filters'
option disabled '1'


config bpfcountd 'eth0_out'
option ifname 'eth0'
option prefilter 'outbound'
option filterfile '/etc/bpfcountd.filters'
option disabled '1'
89 changes: 89 additions & 0 deletions package/bpfcountd/files/etc/init.d/bpfcountd
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/sh /etc/rc.common

USE_PROCD=1
START=20
STOP=90

UNIXSOCKDIR=/var/run/bpfcountd

bpfcountd_start() {
local instance="$1"
local cfg="$2"
local disabled

local ifname
local prefilter
local filterfile

echo "bpfcountd_start"
config_get_bool disabled "$cfg" disabled 0
[ "$disabled" -gt 0 ] && return 0

mkdir -p "$UNIXSOCKDIR"

config_get ifname "$cfg" "ifname"
config_get prefilter "$cfg" "prefilter"
config_get filterfile "$cfg" "filterfile"

[ -z "$ifname" ] && {
echo "Error: no ifname specified for $cfg" >&2
return 0
}
[ -z "$filterfile" ] && {
echo "Error: no filterfile specified for $cfg" >&2
return 0
}

procd_open_instance "$instance"

procd_set_param command /usr/sbin/bpfcountd
procd_append_param command -i "$ifname"
procd_append_param command -f "$filterfile"
procd_append_param command -u $UNIXSOCKDIR/"${instance}".sock
[ -n "$prefilter" ] && procd_append_param command -F "$prefilter"

procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}

procd_set_param stderr 1
procd_close_instance
}

start_service() {
local instance="$1"
local cfg="$2"
local instance_found=0

. /lib/functions/network.sh

[ -z "$cfg" ] && cfg="$instance"

echo "start_service"
config_cb() {
local type="$1"
local name="$2"
if [ "$type" = "bpfcountd" ]; then
if [ -n "$cfg" -a "$cfg" = "$name" ]; then
instance_found=1
fi
fi
}

config_load bpfcountd

if [ -n "$instance" ]; then
[ "$instance_found" -gt 0 ] || return
bpfcountd_start "$instance" "$cfg"
else
config_foreach bpfcountd_start bpfcountd
fi
}

stop_service() {
local cfg="$1"

if [ -n "$cfg" ]; then
rm $UNIXSOCKDIR/$cfg.sock
else
rm $UNIXSOCKDIR/*.sock
fi
}

0 comments on commit cc2cafd

Please sign in to comment.