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