aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2021-07-19 22:41:11 +0200
committerToni Uhlig <matzeton@googlemail.com>2021-07-19 22:41:11 +0200
commit14df89ffb00264b014bca355b2f34941fe4f0d03 (patch)
tree07064045b6685f2319b32f5ecdd81854dea3f488
parent32275543c421eae55fd98a5a98e00059a0407953 (diff)
Added benchmarking support via Google's libbenchmark.add/google-benchmark
* disabled confusing GMake builtin rules which are not used anyway * updated .gitignore Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r--.gitignore14
-rw-r--r--configure.seed14
-rw-r--r--example/Makefile.in1
-rw-r--r--tests/benchmark/Makefile.in49
-rw-r--r--tests/benchmark/benchmark.cpp174
-rw-r--r--tests/dga/Makefile.in8
-rw-r--r--tests/unit/Makefile.in8
7 files changed, 258 insertions, 10 deletions
diff --git a/.gitignore b/.gitignore
index ebacea3cd..c8fba911b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -17,12 +17,22 @@ compile
/depcomp
.dirstamp
/example/.deps/
-/example/Makefile
/example/ndpiReader
/example/Makefile.dpdk
/example/build
-*.in
+# Makefiles generated by configure
+/example/Makefile
+/fuzz/Makefile
+/python/Makefile
+/tests/benchmark/Makefile
+/tests/dga/Makefile
+/tests/unit/Makefile
+# Makefiles generated by AutoMake
+/fuzz/Makefile.in
+/tests/Makefile.in
+/Makefile.in
/install-sh
+*.h.in
*.ko
*.la
/libndpi.pc
diff --git a/configure.seed b/configure.seed
index 3ca8897a7..16b036650 100644
--- a/configure.seed
+++ b/configure.seed
@@ -7,7 +7,16 @@ AM_INIT_AUTOMAKE([foreign subdir-objects])
AC_PREFIX_DEFAULT(/usr)
-EXTRA_TARGETS="example tests tests/unit tests/dga"
+EXTRA_TARGETS="example tests tests/unit tests/dga tests/benchmark"
+
+AC_ARG_WITH(benchmark, AS_HELP_STRING([--with-benchmark], [Build benchmark test. Requires libbenchmark.]))
+AS_IF([test "${with_benchmark+set}" = set],[
+ PKG_CHECK_MODULES([BENCHMARK], [benchmark], [BENCHMARK_LIBS=`pkg-config --libs benchmark` BENCHMARK_CFLAGS=`pkg-config --cflags benchmark`])
+ LIBS="$LIBS $BENCHMARK_LIBS"
+ CFLAGS="$CFLAGS $BENCHMARK_CFLAGS"
+ BUILD_BENCHMARK=1
+])
+
AC_ARG_WITH(only-libndpi, AS_HELP_STRING([--with-only-libndpi], [Build only libndpi (no examples, tests etc)]))
AS_IF([test "${with_only_libndpi+set}" = set],[
EXTRA_TARGETS=""
@@ -236,7 +245,7 @@ if test "${with_maxminddb+set}" = set; then :
fi
fi
-AC_CONFIG_FILES([Makefile example/Makefile example/Makefile.dpdk tests/Makefile tests/unit/Makefile tests/dga/Makefile libndpi.pc src/include/ndpi_define.h src/lib/Makefile python/Makefile fuzz/Makefile src/include/ndpi_api.h])
+AC_CONFIG_FILES([Makefile example/Makefile example/Makefile.dpdk tests/Makefile tests/unit/Makefile tests/benchmark/Makefile tests/dga/Makefile libndpi.pc src/include/ndpi_define.h src/lib/Makefile python/Makefile fuzz/Makefile src/include/ndpi_api.h])
AC_CONFIG_FILES([tests/do.sh], [chmod +x tests/do.sh])
AC_CONFIG_FILES([tests/do_valgrind.sh], [chmod +x tests/do_valgrind.sh])
AC_CONFIG_HEADERS(src/include/ndpi_config.h)
@@ -256,6 +265,7 @@ AC_SUBST(NDPI_API_VERSION)
AC_SUBST(EXTRA_TARGETS)
AC_SUBST(BUILD_MINGW)
AC_SUBST(BUILD_FUZZTARGETS)
+AC_SUBST(BUILD_BENCHMARK)
AC_SUBST(JSONC_CFLAGS)
AC_SUBST(JSONC_LIBS)
AC_SUBST(GCRYPT_ENABLED)
diff --git a/example/Makefile.in b/example/Makefile.in
index ec44acbe3..2197393e5 100644
--- a/example/Makefile.in
+++ b/example/Makefile.in
@@ -1,3 +1,4 @@
+MAKEFLAGS += --no-builtin-rules
CC=@CC@
CXX=@CXX@
BUILD_MINGW=@BUILD_MINGW@
diff --git a/tests/benchmark/Makefile.in b/tests/benchmark/Makefile.in
new file mode 100644
index 000000000..8ce5044b9
--- /dev/null
+++ b/tests/benchmark/Makefile.in
@@ -0,0 +1,49 @@
+MAKEFLAGS += --no-builtin-rules
+BUILD_BENCHMARK=@BUILD_BENCHMARK@
+
+ifneq ($(BUILD_BENCHMARK),)
+
+CC=@CC@
+CXX=@CXX@
+
+SRCHOME=../../src
+
+CXXFLAGS=-g -fPIC -DPIC -I$(SRCHOME)/include -I$(SRCHOME)/../example @PCAP_INC@ @CFLAGS@
+LIBNDPI=$(SRCHOME)/../example/libndpiReader.a $(SRCHOME)/lib/libndpi.a
+LDFLAGS=$(LIBNDPI) @PCAP_LIB@ @LIBS@ @ADDITIONAL_LIBS@ -lpthread -lm @LDFLAGS@
+HEADERS=$(SRCHOME)/include/ndpi_api.h $(SRCHOME)/include/ndpi_typedefs.h $(SRCHOME)/include/ndpi_protocol_ids.h \
+ $(SRCHOME)/../example/reader_util.h
+PREFIX?=@prefix@
+
+all: benchmark
+
+benchmark: $(LIBNDPI) benchmark.o
+ $(CXX) $(CXXFLAGS) benchmark.o -o $@ $(LDFLAGS)
+
+%.o: %.cpp $(HEADERS) Makefile
+ $(CXX) $(CXXFLAGS) -c $< -o $@
+
+clean:
+ /bin/rm -f *.o benchmark
+ /bin/rm -f .*.o.cmd .*.o.d
+ /bin/rm -rf build
+
+install:
+ echo ""
+
+distclean: clean
+ /bin/rm -f Makefile
+
+else
+
+all: benchmark
+benchmark:
+clean:
+install:
+distclean:
+%.o: %.cpp
+
+endif
+
+distdir:
+ find . -maxdepth 1 -type f -name '*.cpp' -o -name '*.h' -o -name '*.py' | xargs -I'{}' cp '{}' '$(distdir)/{}'
diff --git a/tests/benchmark/benchmark.cpp b/tests/benchmark/benchmark.cpp
new file mode 100644
index 000000000..b981bc069
--- /dev/null
+++ b/tests/benchmark/benchmark.cpp
@@ -0,0 +1,174 @@
+extern "C" {
+#include "reader_util.h"
+#include "ndpi_api.h"
+#include <pcap/pcap.h>
+
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+};
+
+struct ndpi_workflow_prefs *prefs = NULL;
+
+int nDPI_LogLevel = 0;
+char *_debug_protocols = NULL;
+u_int32_t current_ndpi_memory = 0, max_ndpi_memory = 0;
+u_int8_t enable_protocol_guess = 1, enable_payload_analyzer = 0;
+u_int8_t enable_joy_stats = 0;
+u_int8_t human_readeable_string_len = 5;
+u_int8_t max_num_udp_dissected_pkts = 16 /* 8 is enough for most protocols, Signal requires more */, max_num_tcp_dissected_pkts = 80 /* due to telnet */;
+
+int bufferToFile(const char * name, const uint8_t *Data, size_t Size) {
+ FILE * fd;
+ if (remove(name) != 0) {
+ if (errno != ENOENT) {
+ perror("remove failed");
+ return -1;
+ }
+ }
+ fd = fopen(name, "wb");
+ if (fd == NULL) {
+ perror("open failed");
+ return -2;
+ }
+ if (fwrite (Data, 1, Size, fd) != Size) {
+ fclose(fd);
+ return -3;
+ }
+ fclose(fd);
+ return 0;
+}
+
+int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
+ pcap_t * pkts;
+ const u_char *pkt;
+ struct pcap_pkthdr *header;
+ int r;
+ char errbuf[PCAP_ERRBUF_SIZE];
+ NDPI_PROTOCOL_BITMASK all;
+ char * pcap_path = tempnam("/tmp", "fuzz-ndpi-reader");
+
+ if (prefs == NULL) {
+ prefs = (struct ndpi_workflow_prefs *)calloc(sizeof(struct ndpi_workflow_prefs), 1);
+ if (prefs == NULL) {
+ //should not happen
+ return 1;
+ }
+ prefs->decode_tunnels = 1;
+ prefs->num_roots = 16;
+ prefs->max_ndpi_flows = 1024;
+ prefs->quiet_mode = 0;
+ }
+ bufferToFile(pcap_path, Data, Size);
+
+ pkts = pcap_open_offline(pcap_path, errbuf);
+ if (pkts == NULL) {
+ remove(pcap_path);
+ free(pcap_path);
+ return 0;
+ }
+ if (ndpi_is_datalink_supported(pcap_datalink(pkts)) == 0)
+ {
+ /* Do not fail if the datalink type is not supported (may happen often during fuzzing). */
+ pcap_close(pkts);
+ remove(pcap_path);
+ free(pcap_path);
+ return 0;
+ }
+ struct ndpi_workflow * workflow = ndpi_workflow_init(prefs, pkts);
+ // enable all protocols
+ NDPI_BITMASK_SET_ALL(all);
+ ndpi_set_protocol_detection_bitmask2(workflow->ndpi_struct, &all);
+ memset(workflow->stats.protocol_counter, 0,
+ sizeof(workflow->stats.protocol_counter));
+ memset(workflow->stats.protocol_counter_bytes, 0,
+ sizeof(workflow->stats.protocol_counter_bytes));
+ memset(workflow->stats.protocol_flows, 0,
+ sizeof(workflow->stats.protocol_flows));
+ ndpi_finalize_initialization(workflow->ndpi_struct);
+
+ header = NULL;
+ r = pcap_next_ex(pkts, &header, &pkt);
+ while (r > 0) {
+ if(header->caplen >= 42 /* ARP+ size */) {
+ /* allocate an exact size buffer to check overflows */
+ uint8_t *packet_checked = (uint8_t *)malloc(header->caplen);
+
+ if(packet_checked) {
+ ndpi_risk flow_risk;
+
+ memcpy(packet_checked, pkt, header->caplen);
+ ndpi_workflow_process_packet(workflow, header, packet_checked, &flow_risk, NULL);
+ free(packet_checked);
+ }
+ }
+
+ r = pcap_next_ex(pkts, &header, &pkt);
+ }
+ ndpi_workflow_free(workflow);
+ pcap_close(pkts);
+
+ remove(pcap_path);
+ free(pcap_path);
+
+ return 0;
+}
+
+int main(int argc, char ** argv)
+{
+ FILE * pcap_file;
+ long pcap_file_size;
+ uint8_t * pcap_buffer;
+ int test_retval;
+
+ if (argc != 2) {
+ fprintf(stderr, "usage: %s: [pcap-file]\n",
+ (argc > 0 ? argv[0] : "fuzz_ndpi_reader_with_main"));
+ return 1;
+ }
+
+ pcap_file = fopen(argv[1], "r");
+ if (pcap_file == NULL) {
+ perror("fopen failed");
+ return 1;
+ }
+
+ if (fseek(pcap_file, 0, SEEK_END) != 0) {
+ perror("fseek(SEEK_END) failed");
+ fclose(pcap_file);
+ return 1;
+ }
+
+ pcap_file_size = ftell(pcap_file);
+ if (pcap_file_size < 0) {
+ perror("ftell failed");
+ fclose(pcap_file);
+ return 1;
+ }
+
+ if (fseek(pcap_file, 0, SEEK_SET) != 0) {
+ perror("fseek(0, SEEK_SET) failed");
+ fclose(pcap_file);
+ return 1;
+ }
+
+ pcap_buffer = (uint8_t *)malloc(pcap_file_size);
+ if (pcap_buffer == NULL) {
+ perror("malloc failed");
+ fclose(pcap_file);
+ return 1;
+ }
+
+ if (fread(pcap_buffer, sizeof(*pcap_buffer), pcap_file_size, pcap_file) != pcap_file_size) {
+ perror("fread failed");
+ fclose(pcap_file);
+ free(pcap_buffer);
+ return 1;
+ }
+
+ test_retval = LLVMFuzzerTestOneInput(pcap_buffer, pcap_file_size);
+ fclose(pcap_file);
+ free(pcap_buffer);
+
+ return test_retval;
+}
diff --git a/tests/dga/Makefile.in b/tests/dga/Makefile.in
index f93424631..db4d91049 100644
--- a/tests/dga/Makefile.in
+++ b/tests/dga/Makefile.in
@@ -1,11 +1,13 @@
+MAKEFLAGS += --no-builtin-rules
+
CC=@CC@
CXX=@CXX@
SRCHOME=../../src
-CFLAGS=-g -fPIC -DPIC -I$(SRCHOME)/include @JSONC_CFLAGS@ @PCAP_INC@ @CFLAGS@
+CFLAGS=-g -fPIC -DPIC -I$(SRCHOME)/include @PCAP_INC@ @CFLAGS@
LIBNDPI=$(SRCHOME)/lib/libndpi.a
-LDFLAGS=$(LIBNDPI) @PCAP_LIB@ @LIBS@ @ADDITIONAL_LIBS@ @JSONC_LIBS@ -lpthread -lm @LDFLAGS@
+LDFLAGS=$(LIBNDPI) @PCAP_LIB@ @LIBS@ @ADDITIONAL_LIBS@ -lpthread -lm @LDFLAGS@
HEADERS=$(SRCHOME)/include/ndpi_api.h $(SRCHOME)/include/ndpi_typedefs.h $(SRCHOME)/include/ndpi_protocol_ids.h
OBJS=dga_evaluate
PREFIX?=@prefix@
@@ -30,7 +32,7 @@ install:
echo ""
distdir:
-
+ find . -maxdepth 1 -type f -name '*.c' -o -name '*.h' -o -name '*.py' | xargs -I'{}' cp '{}' '$(distdir)/{}'
distclean: clean
/bin/rm -f Makefile
diff --git a/tests/unit/Makefile.in b/tests/unit/Makefile.in
index 88f100ae1..61d468444 100644
--- a/tests/unit/Makefile.in
+++ b/tests/unit/Makefile.in
@@ -1,11 +1,13 @@
+MAKEFLAGS += --no-builtin-rules
+
CC=@CC@
CXX=@CXX@
SRCHOME=../../src
-CFLAGS=-g -fPIC -DPIC -I$(SRCHOME)/include @JSONC_CFLAGS@ @PCAP_INC@ @CFLAGS@
+CFLAGS=-g -fPIC -DPIC -I$(SRCHOME)/include @PCAP_INC@ @CFLAGS@
LIBNDPI=$(SRCHOME)/lib/libndpi.a
-LDFLAGS=$(LIBNDPI) @PCAP_LIB@ @LIBS@ @ADDITIONAL_LIBS@ @JSONC_LIBS@ -lpthread -lm @LDFLAGS@
+LDFLAGS=$(LIBNDPI) @PCAP_LIB@ @LIBS@ @ADDITIONAL_LIBS@ -lpthread -lm @LDFLAGS@
HEADERS=$(SRCHOME)/include/ndpi_api.h $(SRCHOME)/include/ndpi_typedefs.h $(SRCHOME)/include/ndpi_protocol_ids.h
OBJS=unit
PREFIX?=@prefix@
@@ -30,7 +32,7 @@ install:
echo ""
distdir:
-
+ find . -maxdepth 1 -type f -name '*.c' -o -name '*.h' -o -name '*.py' | xargs -I'{}' cp '{}' '$(distdir)/{}'
distclean: clean
/bin/rm -f Makefile