Skip to content

Commit

Permalink
Implement min_vni configuration option (#57)
Browse files Browse the repository at this point in the history
* Implement min_vni configuration option

* Format

* Lint

* Format

* Lint

* Format
  • Loading branch information
rgwohlbold authored Apr 11, 2023
1 parent 282ba85 commit d76eb49
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/ConfigParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ std::vector<std::string> ConfigParser::get_config_string_vector(const std::strin
void ConfigParser::populate_configuration(Configuration &config) const {
config.apply_config_file(*this);
config.apply_environment();
config.check_validity();
}
13 changes: 12 additions & 1 deletion src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "filesystem"
#include "logging/loginit.h"

const uint32_t Configuration::DEFAULT_MIN_VNI = 1;
const uint32_t Configuration::DEFAULT_MAX_VNI = 20;
const std::string Configuration::DEFAULT_HAPD_SOCKDIR = "/var/run/hostapd/";
const std::string Configuration::DEFAULT_HAPD_GROUP = "root";
Expand All @@ -14,6 +15,8 @@ const uint32_t Configuration::DEFAULT_CLEANUP_INTERVAL = 10;

const std::string HAPD_GLOBAL_SOCK_NAME = "global";

Configuration Configuration::INSTANCE; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)

static void set_string_if_valid(const ConfigParser& parser, std::string& config_target, const std::string& key) {
try {
config_target = parser.get_config_string(key);
Expand Down Expand Up @@ -55,8 +58,16 @@ void Configuration::apply_config_file(const ConfigParser& parser) {
set_string_if_valid(parser, this->hapd_group, "hapd_group");
set_string_if_valid(parser, this->log_path, "log_path");
set_uint32_if_valid(parser, this->cleanup_interval, "cleanup_interval");
set_uint32_if_valid(parser, this->min_vni, "min_vni");
set_uint32_if_valid(parser, this->max_vni, "max_vni");
set_string_vector_if_valid(parser, this->socknames, "sockets");
}

void Configuration::apply_environment() { set_all_available_sockets_if_empty(); }
void Configuration::apply_environment() { set_all_available_sockets_if_empty(); }

void Configuration::check_validity() const {
if (min_vni > max_vni) {
throw std::runtime_error("min_vni must be less than or equal to max_vni " + std::to_string(min_vni) + " > " +
std::to_string(max_vni));
}
}
17 changes: 12 additions & 5 deletions src/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,27 @@ class ConfigParser;

class Configuration {
public:
static Configuration& get_instance() {
static Configuration instance;
return instance;
}
static Configuration& get_instance() { return INSTANCE; }
static void reset() { INSTANCE = Configuration{}; }

~Configuration() = default;
Configuration(Configuration const&) = delete;
void operator=(Configuration const&) = delete;
Configuration(Configuration&&) = delete;
Configuration& operator=(Configuration const&) = delete;

void apply_config_file(const ConfigParser& parser);
void apply_environment();
void check_validity() const;

static const uint32_t DEFAULT_MIN_VNI;
static const uint32_t DEFAULT_MAX_VNI;
static const std::string DEFAULT_HAPD_SOCKDIR;
static const std::string DEFAULT_HAPD_GROUP;
static const std::string DEFAULT_LOG_PATH;
static const std::vector<std::string> DEFAULT_SOCKNAMES;
static const uint32_t DEFAULT_CLEANUP_INTERVAL;

uint32_t min_vni = DEFAULT_MIN_VNI;
uint32_t max_vni = DEFAULT_MAX_VNI;
std::string hapd_sockdir = DEFAULT_HAPD_SOCKDIR;
std::string hapd_group = DEFAULT_HAPD_GROUP;
Expand All @@ -36,6 +40,9 @@ class Configuration {
private:
Configuration() = default;
void set_all_available_sockets_if_empty();
Configuration& operator=(Configuration&&) = default;

static Configuration INSTANCE; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
};

#include "ConfigParser.h"
Expand Down
8 changes: 7 additions & 1 deletion src/Station.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
Station::Station(std::string sockname, MacAddress mac)
: vlan_id(std::nullopt), mac(std::move(mac)), sockname(std::move(sockname)) {}

uint32_t Station::vni() const { return mac.number() % Configuration::get_instance().max_vni; }
uint32_t Station::vni() const {
if (Configuration::get_instance().min_vni == Configuration::get_instance().max_vni) {
return Configuration::get_instance().min_vni;
}
return mac.number() % (Configuration::get_instance().max_vni - Configuration::get_instance().min_vni) +
Configuration::get_instance().min_vni;
}

std::string Station::vlan_interface_name() const { return "vlan" + std::to_string(vlan_id.value_or(0)); }

Expand Down
14 changes: 13 additions & 1 deletion test/configuration_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
class ConfigurationTest : public testing::Test {
public:
static Configuration& from_string(const std::string& string) {
std::stringstream stream(string);
Configuration::reset();
Configuration& config = Configuration::get_instance();

std::stringstream stream(string);
config.apply_config_file(ConfigParser(stream));
config.check_validity();
return config;
}
};
Expand All @@ -22,11 +25,20 @@ TEST(ConfigurationTest, EmptyConfiguration) {
ASSERT_EQ(config.cleanup_interval, Configuration::DEFAULT_CLEANUP_INTERVAL);
}

TEST(ConfigurationTest, MinVNI) {
Configuration& config = ConfigurationTest::from_string("min_vni=3");
ASSERT_EQ(config.min_vni, 3);
}

TEST(ConfigurationTest, MaxVNI) {
Configuration& config = ConfigurationTest::from_string("max_vni=5");
ASSERT_EQ(config.max_vni, 5);
}

TEST(ConfigurationTest, MinVNIGreaterThanMaxVNI) {
EXPECT_THROW(ConfigurationTest::from_string("min_vni=6\nmax_vni=5"), std::runtime_error);
}

TEST(ConfigurationTest, HapdGroup) {
Configuration& config = ConfigurationTest::from_string("hapd_group=network");
ASSERT_EQ(config.hapd_group, "network");
Expand Down
28 changes: 28 additions & 0 deletions test/station_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "Station.h"

#include <gtest/gtest.h>

#include "Configuration.h"

class StationTest : public testing::TestWithParam<std::tuple<MacAddress, uint32_t, uint32_t>> {};

TEST_P(StationTest, VniIsWithinRange) {
auto [mac, min_vni, max_vni] = StationTest::GetParam();
if (min_vni > max_vni) {
return;
}
Configuration &config = Configuration::get_instance();
config.min_vni = min_vni;
config.max_vni = max_vni;
Station station("wlan0", mac);
ASSERT_LE(min_vni, station.vni());
ASSERT_GE(max_vni, station.vni());
}

INSTANTIATE_TEST_SUITE_P(
ExampleStationData, StationTest,
testing::Combine(testing::Values(MacAddress("00:00:00:00:00:00"), MacAddress("00:00:00:00:00:01"),
MacAddress("00:00:00:00:00:0F"), MacAddress("00:00:00:00:00:20"),
MacAddress("00:00:00:00:04:00"), MacAddress("00:00:00:00:80:00"),
MacAddress("01:00:00:00:00:00")),
testing::Values(0, 1, 2, 3, 4, 5), testing::Values(0, 1, 2, 3, 4, 5)));

0 comments on commit d76eb49

Please sign in to comment.