Implement new logging system.
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#include <loader/utils/cmdline.h>
|
||||
#include <loader/utils/config.h>
|
||||
#include <loader/utils/xdp.h>
|
||||
#include <loader/utils/logging.h>
|
||||
#include <loader/utils/stats.h>
|
||||
#include <loader/utils/helpers.h>
|
||||
|
||||
@@ -37,16 +38,6 @@ int main(int argc, char *argv[])
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// Raise RLimit.
|
||||
struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
|
||||
|
||||
if (setrlimit(RLIMIT_MEMLOCK, &rl))
|
||||
{
|
||||
fprintf(stderr, "[ERROR] Failed to raise rlimit. Please make sure this program is ran as root!\n");
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Initialize config.
|
||||
config__t cfg = {0};
|
||||
|
||||
@@ -68,54 +59,87 @@ int main(int argc, char *argv[])
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
LogMsg(&cfg, 2, 0, "Raising RLimit...");
|
||||
|
||||
// Raise RLimit.
|
||||
struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
|
||||
|
||||
if (setrlimit(RLIMIT_MEMLOCK, &rl))
|
||||
{
|
||||
LogMsg(&cfg, 0, 1, "[ERROR] Failed to raise rlimit. Please make sure this program is ran as root!\n");
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
LogMsg(&cfg, 2, 0, "Retrieving interface index for '%s'...", cfg.interface);
|
||||
|
||||
// Get interface index.
|
||||
int ifidx = if_nametoindex(cfg.interface);
|
||||
|
||||
if (ifidx < 0)
|
||||
{
|
||||
fprintf(stderr, "[ERROR] Failed to retrieve index of network interface '%s'.\n", cfg.interface);
|
||||
LogMsg(&cfg, 0, 1, "[ERROR] Failed to retrieve index of network interface '%s'.\n", cfg.interface);
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
LogMsg(&cfg, 2, 0, "Loading XDP/BPF program at '%s'...", XDP_OBJ_PATH);
|
||||
|
||||
// Load BPF object.
|
||||
struct xdp_program *prog = LoadBpfObj(XDP_OBJ_PATH);
|
||||
|
||||
if (prog == NULL)
|
||||
{
|
||||
fprintf(stderr, "[ERROR] Failed to load eBPF object file. Object path => %s.\n", XDP_OBJ_PATH);
|
||||
LogMsg(&cfg, 0, 1, "[ERROR] Failed to load eBPF object file. Object path => %s.\n", XDP_OBJ_PATH);
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
LogMsg(&cfg, 2, 0, "Attaching XDP program to interface '%s'...", cfg.interface);
|
||||
|
||||
// Attach XDP program.
|
||||
if ((ret = AttachXdp(prog, ifidx, 0, &cmd)) != 0)
|
||||
char *mode_used = NULL;
|
||||
|
||||
if ((ret = AttachXdp(prog, &mode_used, ifidx, 0, &cmd)) != 0)
|
||||
{
|
||||
fprintf(stderr, "[ERROR] Failed to attach XDP program to interface '%s' (%d).\n", cfg.interface, ret);
|
||||
LogMsg(&cfg, 0, 1, "[ERROR] Failed to attach XDP program to interface '%s' using available modes (%d).\n", cfg.interface, ret);
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (mode_used != NULL)
|
||||
{
|
||||
LogMsg(&cfg, 1, 0, "Attached XDP program using mode '%s'...", mode_used);
|
||||
}
|
||||
|
||||
LogMsg(&cfg, 2, 0, "Retrieving BPF map FDs...");
|
||||
|
||||
// Retrieve BPF maps.
|
||||
int filters_map = FindMapFd(prog, "filters_map");
|
||||
|
||||
// Check for valid maps.
|
||||
if (filters_map < 0)
|
||||
{
|
||||
fprintf(stderr, "[ERROR] Failed to find 'filters_map' BPF map.\n");
|
||||
LogMsg(&cfg, 0, 1, "[ERROR] Failed to find 'filters_map' BPF map.\n");
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
LogMsg(&cfg, 3, 0, "filters_map FD => %d.", filters_map);
|
||||
|
||||
int stats_map = FindMapFd(prog, "stats_map");
|
||||
|
||||
if (stats_map < 0)
|
||||
{
|
||||
fprintf(stderr, "[ERROR] Failed to find 'stats_map' BPF map.\n");
|
||||
LogMsg(&cfg, 0, 1, "[ERROR] Failed to find 'stats_map' BPF map.\n");
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
LogMsg(&cfg, 3, 0, "stats_map FD => %d.", stats_map);
|
||||
|
||||
LogMsg(&cfg, 2, 0, "Updating filters...");
|
||||
|
||||
// Update BPF maps.
|
||||
UpdateFilters(filters_map, &cfg);
|
||||
|
||||
@@ -126,6 +150,8 @@ int main(int argc, char *argv[])
|
||||
// Receive CPU count for stats map parsing.
|
||||
int cpus = get_nprocs_conf();
|
||||
|
||||
LogMsg(&cfg, 4, 0, "Retrieved %d CPUs on host.", cpus);
|
||||
|
||||
unsigned int end_time = (cmd.time > 0) ? time(NULL) + cmd.time : 0;
|
||||
|
||||
// Create last updated variables.
|
||||
@@ -159,7 +185,7 @@ int main(int argc, char *argv[])
|
||||
// Update config.
|
||||
if ((ret = LoadConfig(&cfg, cmd.cfgfile)) != 0)
|
||||
{
|
||||
fprintf(stderr, "[WARNING] Failed to load config after update check (%d)...\n", ret);
|
||||
LogMsg(&cfg, 1, 0, "[WARNING] Failed to load config after update check (%d)...\n", ret);
|
||||
}
|
||||
|
||||
// Update BPF maps.
|
||||
@@ -178,7 +204,7 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
if (CalculateStats(stats_map, cpus))
|
||||
{
|
||||
fprintf(stderr, "[WARNING] Failed to calculate packet stats. Stats map FD => %d...\n", stats_map);
|
||||
LogMsg(&cfg, 1, 0, "[WARNING] Failed to calculate packet stats. Stats map FD => %d...\n", stats_map);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,14 +214,14 @@ int main(int argc, char *argv[])
|
||||
fprintf(stdout, "\n");
|
||||
|
||||
// Detach XDP program.
|
||||
if (AttachXdp(prog, ifidx, 1, &cmd))
|
||||
if (AttachXdp(prog, &mode_used, ifidx, 1, &cmd))
|
||||
{
|
||||
fprintf(stderr, "[ERROR] Failed to detach XDP program from interface '%s'.\n", cfg.interface);
|
||||
LogMsg(&cfg, 0, 1, "[ERROR] Failed to detach XDP program from interface '%s'.\n", cfg.interface);
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
fprintf(stdout, "Cleaned up and exiting...\n");
|
||||
LogMsg(&cfg, 1, 0, "Cleaned up and exiting...\n");
|
||||
|
||||
// Exit program successfully.
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
Reference in New Issue
Block a user