diff mbox series

[bpf-next,v3,03/16] bpfilter: add logging facility

Message ID 20221224000402.476079-4-qde@naccy.de (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series bpfilter | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 fail Logs for test_progs_no_alu32 on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_progs_no_alu32_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_progs_no_alu32_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for test_progs_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-31 success Logs for test_progs_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-32 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-34 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 success Logs for test_verifier on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-36 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-37 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-38 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_maps on s390x with gcc
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count fail Series longer than 15 patches (and no cover letter)
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 7 of 7 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ${{ matrix.test }} on ${{ matrix.arch }} with ${{ matrix.toolchain }}
bpf/vmtest-bpf-next-VM_Test-2 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-3 fail Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 fail Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-5 fail Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-6 fail Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 fail Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-8 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-9 success Logs for set-matrix

Commit Message

Quentin Deslandes Dec. 24, 2022, 12:03 a.m. UTC
bpfilter will log to /dev/kmsg by default. Four different log levels are
available. LOG_EMERG() will exit the usermode helper after logging.

Signed-off-by: Quentin Deslandes <qde@naccy.de>
---
 net/bpfilter/Makefile |  2 +-
 net/bpfilter/logger.c | 52 ++++++++++++++++++++++++++++
 net/bpfilter/logger.h | 80 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 133 insertions(+), 1 deletion(-)
 create mode 100644 net/bpfilter/logger.c
 create mode 100644 net/bpfilter/logger.h
diff mbox series

Patch

diff --git a/net/bpfilter/Makefile b/net/bpfilter/Makefile
index cdac82b8c53a..8d9c726ba1a5 100644
--- a/net/bpfilter/Makefile
+++ b/net/bpfilter/Makefile
@@ -4,7 +4,7 @@ 
 #
 
 userprogs := bpfilter_umh
-bpfilter_umh-objs := main.o
+bpfilter_umh-objs := main.o logger.o
 userccflags += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi
 
 ifeq ($(CONFIG_BPFILTER_UMH), y)
diff --git a/net/bpfilter/logger.c b/net/bpfilter/logger.c
new file mode 100644
index 000000000000..c256bfef7e6c
--- /dev/null
+++ b/net/bpfilter/logger.c
@@ -0,0 +1,52 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
+ */
+
+#include "logger.h"
+
+#include <errno.h>
+
+static const char *log_file_path = "/dev/kmsg";
+static FILE *log_file;
+
+int logger_init(void)
+{
+	if (log_file)
+		return 0;
+
+	log_file = fopen(log_file_path, "w");
+	if (!log_file)
+		return -errno;
+
+	if (setvbuf(log_file, 0, _IOLBF, 0))
+		return -errno;
+
+	return 0;
+}
+
+void logger_set_file(FILE *file)
+{
+	log_file = file;
+}
+
+FILE *logger_get_file(void)
+{
+	return log_file;
+}
+
+int logger_clean(void)
+{
+	int r;
+
+	if (!log_file)
+		return 0;
+
+	r = fclose(log_file);
+	if (r == EOF)
+		return -errno;
+
+	log_file = NULL;
+
+	return 0;
+}
diff --git a/net/bpfilter/logger.h b/net/bpfilter/logger.h
new file mode 100644
index 000000000000..c44739ec0069
--- /dev/null
+++ b/net/bpfilter/logger.h
@@ -0,0 +1,80 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
+ */
+
+#ifndef NET_BPFILTER_LOGGER_H
+#define NET_BPFILTER_LOGGER_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+
+#define _BFLOG_IMPL(level, fmt, ...)					      \
+	do {								      \
+		typeof(level) __level = level;				      \
+		if (logger_get_file()) {				      \
+			fprintf(logger_get_file(), "<%d>bpfilter: " fmt "\n", \
+				(__level), ##__VA_ARGS__);		      \
+		}							      \
+		if ((__level) == LOG_EMERG)				      \
+			exit(EXIT_FAILURE);				      \
+	} while (0)
+
+#define BFLOG_EMERG(fmt, ...) \
+	_BFLOG_IMPL(LOG_KERN | LOG_EMERG, fmt, ##__VA_ARGS__)
+#define BFLOG_ERR(fmt, ...) \
+	_BFLOG_IMPL(LOG_KERN | LOG_ERR, fmt, ##__VA_ARGS__)
+#define BFLOG_NOTICE(fmt, ...) \
+	_BFLOG_IMPL(LOG_KERN | LOG_NOTICE, fmt, ##__VA_ARGS__)
+
+#ifdef DEBUG
+#define BFLOG_DBG(fmt, ...) BFLOG_IMPL(LOG_KERN | LOG_DEBUG, fmt, ##__VA_ARGS__)
+#else
+#define BFLOG_DBG(fmt, ...)
+#endif
+
+#define STRERR(v) strerror(abs(v))
+
+/**
+ * logger_init() - Initialise logging facility.
+ *
+ * This function is used to open a file to write logs to (see @log_file_path).
+ * It must be called before using any logging macro, otherwise log messages
+ * will be discarded.
+ *
+ * Return: 0 on success, negative errno value on error.
+ */
+int logger_init(void);
+
+/**
+ * logger_set_file() - Set the FILE pointer to use to log messages.
+ * @file: new FILE * to the log file.
+ *
+ * This function won't check whether the FILE pointer is valid, nor whether
+ * a file is already opened, this is the responsibility of the caller. Once
+ * logger_set_file() returns, all new log messages will be printed to the
+ * FILE * provided.
+ */
+void logger_set_file(FILE *file);
+
+/**
+ * logger_get_file() - Returns a FILE * pointer to the log file.
+ *
+ * Return: pointer to the file to log to (as a FILE *), or NULL if the file
+ *	is not valid.
+ */
+FILE *logger_get_file(void);
+
+/**
+ * logger_clean() - Close the log file.
+ *
+ * On success, the log file pointer will be NULL. If the function fails,
+ * the log file pointer remain unchanged and the file should be considered open.
+ *
+ * Return: 0 on success, negative errno value on error.
+ */
+int logger_clean(void);
+
+#endif // NET_BPFILTER_LOGGER_H