diff --git a/common/controlplaneconfig.h b/common/controlplaneconfig.h index da9a6d4d..6458619f 100644 --- a/common/controlplaneconfig.h +++ b/common/controlplaneconfig.h @@ -3,6 +3,7 @@ #include #include "balancer.h" +#include "define.h" #include "scheduler.h" #include "type.h" @@ -104,6 +105,19 @@ class interface_t common::globalBase::tFlow flow; }; +class bird_import_t +{ +public: + SERIALIZABLE(socket, vrf); + +public: + inline static const std::string socketStr = "socket"; + inline static const std::string vrfStr = "vrf"; + + std::string socket; + std::string vrf; +}; + class config_t { public: @@ -114,7 +128,7 @@ class config_t nlohmann::json save() const; */ - SERIALIZABLE(routeId, to_kernel_prefixes, vrf, tunnel_enabled, ignore_tables, ipv4_source_address, ipv6_source_address, udp_destination_port, local_prefixes, peers, interfaces); + SERIALIZABLE(routeId, to_kernel_prefixes, vrf, tunnel_enabled, ignore_tables, ipv4_source_address, ipv6_source_address, udp_destination_port, local_prefixes, peers, interfaces, bird_imports); public: tRouteId routeId; @@ -128,6 +142,7 @@ class config_t std::set local_prefixes; ///< for fallback to default std::map peers; std::map interfaces; + std::vector bird_imports; }; } diff --git a/controlplane/configparser.cpp b/controlplane/configparser.cpp index 92054e4c..b0352697 100644 --- a/controlplane/configparser.cpp +++ b/controlplane/configparser.cpp @@ -197,6 +197,32 @@ controlplane::base_t config_parser_t::loadConfig(const std::string& rootFilePath return baseNext; } +void config_parser_t::loadConfig_route_bird(controlplane::base_t& baseNext, + std::vector& birdsImport, + const nlohmann::json& birdJson) +{ + using BirdImport = controlplane::route::bird_import_t; + for (const auto& elemJson : birdJson) + { + BirdImport import; + + if (exist(elemJson, BirdImport::socketStr)) + { + import.socket = elemJson[BirdImport::socketStr]; + } + + if (exist(elemJson, BirdImport::vrfStr)) + { + import.vrf = elemJson[BirdImport::vrfStr]; + } + + birdsImport.push_back(import); + YANET_LOG_INFO("loadConfig_route_bird: socket(%s), vrf(%s)\n", + import.socket.data(), + import.vrf.data()); + } +} + void config_parser_t::loadConfig_logicalPort(controlplane::base_t& baseNext, const std::string& moduleId, const nlohmann::json& moduleJson) @@ -367,6 +393,13 @@ void config_parser_t::loadConfig_route(controlplane::base_t& baseNext, route.tunnel_enabled = false; } + if (exist(moduleJson, "birdImport")) + { + loadConfig_route_bird(baseNext, + route.bird_imports, + moduleJson["birdImport"]); + } + // route.routeId = routeId; diff --git a/controlplane/configparser.h b/controlplane/configparser.h index 2b86dad8..df6ee28e 100644 --- a/controlplane/configparser.h +++ b/controlplane/configparser.h @@ -21,6 +21,7 @@ class config_parser_t void loadConfig_logicalPort(controlplane::base_t& baseNext, const std::string& moduleId, const nlohmann::json& moduleJson); void loadConfig_route(controlplane::base_t& baseNext, const std::string& moduleId, const nlohmann::json& moduleJson, const std::string& rootFilePath, const std::map& jsons); void loadConfig_route_peers(controlplane::base_t& baseNext, controlplane::route::config_t& route, const nlohmann::json& json, const std::string& rootFilePath, const std::map& jsons); + void loadConfig_route_bird(controlplane::base_t& baseNext, std::vector& birdsImport, const nlohmann::json& birdJson); void loadConfig_decap(controlplane::base_t& baseNext, const std::string& moduleId, const nlohmann::json& moduleJson); void loadConfig_nat64stateful(controlplane::base_t& baseNext, const std::string& moduleId, const nlohmann::json& moduleJson, const std::string& rootFilePath, const std::map& jsons); void loadConfig_nat64stateless(controlplane::base_t& baseNext, const std::string& moduleId, const nlohmann::json& moduleJson, const std::string& rootFilePath, const std::map& jsons); diff --git a/controlplane/controlplane.cpp b/controlplane/controlplane.cpp index ae31b56d..16ff417f 100644 --- a/controlplane/controlplane.cpp +++ b/controlplane/controlplane.cpp @@ -538,6 +538,18 @@ common::icp::controlplane_values::response cControlPlane::controlplane_values() return response; } +common::icp::route_config::response cControlPlane::getRoute() const +{ + common::icp::route_config::response response; + + { + auto current_guard = generations.current_lock_guard(); + response = generations.current().routes; + } + + return response; +} + common::icp::getDecapPrefixes::response cControlPlane::command_getDecapPrefixes() { common::icp::getDecapPrefixes::response response; @@ -941,6 +953,14 @@ eResult cControlPlane::loadConfig(const std::string& rootFilePath, } YANET_LOG_INFO("dataplane has been updated (stage 7)\n"); + for (auto& module : modules) + { + if (rib_t* rib = dynamic_cast(module)) + { + rib->bird_import_get(); + rib->moduleStart(); + } + } } catch (const error_result_t& error) { diff --git a/controlplane/controlplane.h b/controlplane/controlplane.h index 7f98d1a6..7e807a05 100644 --- a/controlplane/controlplane.h +++ b/controlplane/controlplane.h @@ -106,6 +106,7 @@ class cControlPlane common::icp::acl_unwind::response acl_unwind(const common::icp::acl_unwind::request& request) const; common::icp::acl_lookup::response acl_lookup(const common::icp::acl_lookup::request& request) const; common::icp::controlplane_values::response controlplane_values() const; + common::icp::route_config::response getRoute() const; common::icp::getDecapPrefixes::response command_getDecapPrefixes(); common::icp::getNat64statelessTranslations::response command_getNat64statelessTranslations(); diff --git a/controlplane/rib.cpp b/controlplane/rib.cpp index 7c40b9c0..53576cc7 100644 --- a/controlplane/rib.cpp +++ b/controlplane/rib.cpp @@ -1,6 +1,6 @@ #include "rib.h" -#include "libbird.h" #include "controlplane.h" +#include "libbird.h" #include @@ -64,10 +64,6 @@ eResult rib_t::init() rib_thread(); }); - funcThreads.emplace_back([this]() { - bird_thread(); - }); - return eResult::success; } @@ -869,22 +865,41 @@ void rib_t::rib_thread() std::this_thread::sleep_for(std::chrono::milliseconds{200}); } +} +void rib_t::bird_import_get() +{ + auto route = controlPlane->getRoute(); + for (auto& [vrf, response] : route) + { + (void)vrf; + auto imports = response.bird_imports; + + for (auto& import : imports) + { + funcThreads.emplace_back([this, import]() { + bird_thread(import.socket, import.vrf); + }); + } + } } -void rib_t::bird_thread() +void rib_t::bird_thread(const std::string& socket, const std::string& vrf) { - while (!flagStop) { - read_bird_feed("/tmp/export.sock", "default", this); + YANET_LOG_DEBUG("Run bird thread: socket(%s), vrf(%s)\n", + socket.data(), + vrf.data()); + while (!flagStop) + { + read_bird_feed(socket.c_str(), vrf.c_str(), this); common::icp::rib_update::clear request = {"bgp", std::nullopt}; -/* std::get<1>(request) = {peer_address, - {"default", ///< @todo: vrf - YANET_RIB_PRIORITY_DEFAULT}}; -*/ + /* std::get<1>(request) = {peer_address, + {"default", ///< @todo: vrf + YANET_RIB_PRIORITY_DEFAULT}}; + */ rib_clear(request); std::this_thread::sleep_for(std::chrono::milliseconds{200}); } - } diff --git a/controlplane/rib.h b/controlplane/rib.h index d0bc45ca..b16bd576 100644 --- a/controlplane/rib.h +++ b/controlplane/rib.h @@ -34,6 +34,8 @@ class rib_t : public cModule common::icp::rib_summary::response rib_summary(); common::icp::rib_prefixes::response rib_prefixes(); + void bird_import_get(); + common::icp::rib_lookup::response rib_lookup(const common::icp::rib_lookup::request& request); common::icp::rib_get::response rib_get(const common::icp::rib_get::request& request); common::icp::rib_save::response rib_save(); @@ -46,7 +48,7 @@ class rib_t : public cModule void rib_eor(const common::icp::rib_update::eor& request); void rib_thread(); - void bird_thread(); + void bird_thread(const std::string& socket, const std::string& vrf); protected: mutable std::mutex rib_update_mutex;