Improve generic code.
This commit is contained in:
@@ -64,6 +64,7 @@ void UpdateFilters(config__t *cfg)
|
||||
|
||||
// Create value array (max CPUs in size) since we're using a per CPU map.
|
||||
filter_t filter[MAX_CPUS];
|
||||
memset(filter, 0, sizeof(filter));
|
||||
|
||||
for (int j = 0; j < MAX_CPUS; j++)
|
||||
{
|
||||
@@ -79,14 +80,14 @@ void UpdateFilters(config__t *cfg)
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves an update from the config.
|
||||
* Loads the config on the file system.
|
||||
*
|
||||
* @param cfg A pointer to the config structure.
|
||||
* @param cfgfile The path to the config file.
|
||||
*
|
||||
* @return 0 on success or -1 on error.
|
||||
*/
|
||||
int UpdateConfig(config__t *cfg, char *cfgfile)
|
||||
int LoadConfig(config__t *cfg, char *cfgfile)
|
||||
{
|
||||
// Open config file.
|
||||
if (OpenCfg(cfgfile) != 0)
|
||||
@@ -98,10 +99,7 @@ int UpdateConfig(config__t *cfg, char *cfgfile)
|
||||
|
||||
SetCfgDefaults(cfg);
|
||||
|
||||
for (u16 i = 0; i < MAX_FILTERS; i++)
|
||||
{
|
||||
cfg->filters[i] = (struct filter) {0};
|
||||
}
|
||||
memset(cfg->filters, 0, sizeof(cfg->filters));
|
||||
|
||||
// Read config and check for errors.
|
||||
if (ReadCfg(cfg) != 0)
|
||||
@@ -320,7 +318,7 @@ int main(int argc, char *argv[])
|
||||
SetCfgDefaults(&cfg);
|
||||
|
||||
// Update config.
|
||||
UpdateConfig(&cfg, cmd.cfgfile);
|
||||
LoadConfig(&cfg, cmd.cfgfile);
|
||||
|
||||
// Check for list option.
|
||||
if (cmd.list)
|
||||
@@ -499,12 +497,12 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
// Check if config file have been modified
|
||||
if (stat(cmd.cfgfile, &conf_stat) == 0 && conf_stat.st_mtime > lastupdated) {
|
||||
// Memleak fix for strdup() in UpdateConfig()
|
||||
// Memleak fix for strdup() in LoadConfig()
|
||||
// Before updating it again, we need to free the old return value
|
||||
free(cfg.interface);
|
||||
|
||||
// Update config.
|
||||
UpdateConfig(&cfg, cmd.cfgfile);
|
||||
LoadConfig(&cfg, cmd.cfgfile);
|
||||
|
||||
// Update BPF maps.
|
||||
UpdateFilters(&cfg);
|
||||
@@ -521,8 +519,9 @@ int main(int argc, char *argv[])
|
||||
if (!cfg.nostats)
|
||||
{
|
||||
u32 key = 0;
|
||||
|
||||
stats_t stats[MAX_CPUS];
|
||||
//memset(stats, 0, sizeof(struct stats) * MAX_CPUS);
|
||||
memset(stats, 0, sizeof(stats));
|
||||
|
||||
u64 allowed = 0;
|
||||
u64 dropped = 0;
|
||||
|
||||
@@ -10,7 +10,7 @@ struct
|
||||
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
|
||||
__uint(max_entries, MAX_FILTERS);
|
||||
__type(key, u32);
|
||||
__type(value, struct filter);
|
||||
__type(value, filter_t);
|
||||
} filters_map SEC(".maps");
|
||||
|
||||
struct
|
||||
@@ -18,7 +18,7 @@ struct
|
||||
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
|
||||
__uint(max_entries, 1);
|
||||
__type(key, u32);
|
||||
__type(value, struct stats);
|
||||
__type(value, stats_t);
|
||||
} stats_map SEC(".maps");
|
||||
|
||||
struct
|
||||
@@ -26,11 +26,11 @@ struct
|
||||
__uint(type, BPF_MAP_TYPE_LRU_HASH);
|
||||
__uint(max_entries, MAX_TRACK_IPS);
|
||||
#ifdef USE_FLOW_RL
|
||||
__type(key, struct flow);
|
||||
__type(key, flow_t);
|
||||
#else
|
||||
__type(key, u32);
|
||||
#endif
|
||||
__type(value, struct ip_stats);
|
||||
__type(value, ip_stats_t);
|
||||
} ip_stats_map SEC(".maps");
|
||||
|
||||
struct
|
||||
@@ -46,11 +46,11 @@ struct
|
||||
__uint(type, BPF_MAP_TYPE_LRU_HASH);
|
||||
__uint(max_entries, MAX_TRACK_IPS);
|
||||
#ifdef USE_FLOW_RL
|
||||
__type(key, struct flow6);
|
||||
__type(key, flow6_t);
|
||||
#else
|
||||
__type(key, u128);
|
||||
#endif
|
||||
__type(value, struct ip_stats);
|
||||
__type(value, ip_stats_t);
|
||||
} ip6_stats_map SEC(".maps");
|
||||
|
||||
struct
|
||||
|
||||
Reference in New Issue
Block a user