diff --git a/README.md b/README.md index 46dc426..0283811 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,7 @@ The following table quickly explains the data types used within the configuratio ### Main | Name | Type | Default | Description | | ---- | ---- | ------- | ----------- | -| verbose | int | `1` | The verbose level for logging (0 - 4 supported so far). | +| verbose | int | `1` | The verbose level for logging (0 - 5 supported so far). | | log_file | string | `/var/log/xdpfw/xdpfw.log` | The log file location. If the string is empty (`""`), the log file is disabled. | | interface | string | `NULL` | The network interface name to attach the XDP program to (usually retrieved with `ip a` or `ifconfig`). | | update_time | uint | `0` | How often to update the config and filtering rules from the file system in seconds (0 disables). | @@ -316,6 +316,11 @@ I recommend only enabling filter logging at this time for debugging. If you'd li I will most likely implement functionality to rate limit log messages from XDP in the future. +### LibBPF Logging +When loading the BPF/XDP program through LibXDP/LibBPF, logging is disabled unless if the `verbose` log setting is set to `5` or higher. + +If the tool fails to load or attach the XDP program, it is recommended you set `verbose` to 5 or above so LibXDP outputs specific warnings and errors. + ## My Other XDP Projects I just wanted to share other open source projects I've made which also utilize XDP (or AF_XDP sockets) for those interested. I hope code from these other projects help programmers trying to utilize XDP in their own projects! diff --git a/src/loader/prog.c b/src/loader/prog.c index 4f4e055..60b9f9c 100644 --- a/src/loader/prog.c +++ b/src/loader/prog.c @@ -85,8 +85,18 @@ int main(int argc, char *argv[]) LogMsg(&cfg, 2, 0, "Loading XDP/BPF program at '%s'...", XDP_OBJ_PATH); + // Determine custom LibBPF log level. + int silent = 1; + + if (cfg.verbose > 4) + { + silent = 0; + } + + SetLibBPFLogMode(silent); + // Load BPF object. - struct xdp_program *prog = LoadBpfObj(XDP_OBJ_PATH); + struct xdp_program *prog = LoadBpfObj(XDP_OBJ_PATH, strict); if (prog == NULL) { diff --git a/src/loader/utils/xdp.c b/src/loader/utils/xdp.c index 9e3184e..3c0dbb8 100644 --- a/src/loader/utils/xdp.c +++ b/src/loader/utils/xdp.c @@ -36,14 +36,44 @@ int FindMapFd(struct xdp_program *prog, const char *map_name) return fd; } +/** + * Custom print function for LibBPF that doesn't print anything (silent mode). + * + * @param level The current LibBPF log level. + * @param format The message format. + * @param args Format arguments for the message. + * + * @return void + */ +static int silent_libbpf_log(enum libbpf_print_level level, const char *format, va_list args) +{ + return 0; +} + +/** + * Sets custom LibBPF log mode. + * + * @param silent If 1, disables LibBPF logging entirely. + * + * @return void + */ +void SetLibBPFLogMode(int silent) +{ + if (silent) + { + libbpf_set_print(silent_libbpf_log); + } +} + /** * Loads a BPF object file. * * @param file_name The path to the BPF object file. + * @param strict Whether to enable strict mode. * * @return XDP program structure (pointer) or NULL. */ -struct xdp_program *LoadBpfObj(const char *file_name) +struct xdp_program *LoadBpfObj(const char *file_name, int strict) { struct xdp_program *prog = xdp_program__open_file(file_name, "xdp_prog", NULL); diff --git a/src/loader/utils/xdp.h b/src/loader/utils/xdp.h index 6b7ed6c..03e3516 100644 --- a/src/loader/utils/xdp.h +++ b/src/loader/utils/xdp.h @@ -11,6 +11,7 @@ #define XDP_OBJ_PATH "/etc/xdpfw/xdp_prog.o" int FindMapFd(struct xdp_program *prog, const char *map_name); -struct xdp_program *LoadBpfObj(const char *file_name); +void SetLibBPFLogMode(int silent); +struct xdp_program *LoadBpfObj(const char *file_name, int strict); int AttachXdp(struct xdp_program *prog, char** mode, int ifidx, u8 detach, cmdline_t *cmd); void UpdateFilters(int filters_map, config__t *cfg); \ No newline at end of file