From 75d3721554764b290febcd80ed1fde928d9ba97e Mon Sep 17 00:00:00 2001 From: Madhu Challa Date: Tue, 21 May 2024 10:30:21 -0700 Subject: [PATCH] Add enable config option for startup section "opflex": { "startup": { "enabled": "true", with default being false. If false none of the startup related code will be executed. Also create a startup subdir ${VARDIR}/lib/opflex-agent-ovs/startup Signed-off-by: Madhu Challa --- agent-ovs/lib/Agent.cpp | 13 +++++++++++-- agent-ovs/lib/include/opflexagent/Agent.h | 1 + docker/travis/launch-opflexagent.sh | 1 + libopflex/engine/Processor.cpp | 15 +++++++++++++-- .../engine/include/opflex/engine/Processor.h | 8 ++++++++ libopflex/include/opflex/ofcore/OFFramework.h | 2 ++ libopflex/ofcore/OFFramework.cpp | 5 +++-- 7 files changed, 39 insertions(+), 6 deletions(-) diff --git a/agent-ovs/lib/Agent.cpp b/agent-ovs/lib/Agent.cpp index 85ecccd6..b5ee0e10 100644 --- a/agent-ovs/lib/Agent.cpp +++ b/agent-ovs/lib/Agent.cpp @@ -77,7 +77,8 @@ Agent::Agent(OFFramework& framework_, const LogParams& _logParams) prometheusExposeLocalHostOnly(false), prometheusExposeEpSvcNan(false), behaviorL34FlowsWithoutSubnet(true), - logParams(_logParams) { + logParams(_logParams), + startupPolicyEnabled(false) { std::random_device rng; std::mt19937 urng(rng()); uuid = to_string(basic_random_generator(urng)()); @@ -189,6 +190,7 @@ void Agent::setProperties(const boost::property_tree::ptree& properties) { static const std::string BEHAVIOR_L34FLOWS_WITHOUT_SUBNET("behavior.l34flows-without-subnet"); static const std::string OPFLEX_ASYC_JSON("opflex.asyncjson.enabled"); static const std::string OVS_ASYNC_JSON("ovs.asyncjson.enabled"); + static const std::string OPFLEX_STARTUP_POLICY_ENABLED("opflex.startup.enabled"); static const std::string OPFLEX_POLICY_FILE("opflex.startup.policy-file"); static const std::string OPFLEX_LOCAL_RESOLVE_AFTER_CONNECTION("opflex.startup.resolve-aft-conn"); static const std::string OPFLEX_STARTUP_POLICY_DURATION("opflex.startup.policy-duration"); @@ -527,6 +529,13 @@ void Agent::setProperties(const boost::property_tree::ptree& properties) { setenv("OVS_USE_ASYNC_JSON", "", true); } + optional startupPolicy = + properties.get_optional(OPFLEX_STARTUP_POLICY_ENABLED); + if (startupPolicy && startupPolicy.get() == true) { + startupPolicyEnabled = true; + LOG(INFO) << "Startup policy is enabled"; + } + optional policyFile = properties.get_optional(OPFLEX_POLICY_FILE); if (policyFile) { @@ -606,7 +615,7 @@ void Agent::applyProperties() { framework.setHandshakeTimeout(peerHandshakeTimeout); framework.setKeepaliveTimeout(keepaliveTimeout); framework.setStartupPolicy(opflexPolicyFile, modelgbp::getMetadata(), - startupPolicyDuration, + startupPolicyDuration, startupPolicyEnabled, localResolveAftConn); } diff --git a/agent-ovs/lib/include/opflexagent/Agent.h b/agent-ovs/lib/include/opflexagent/Agent.h index 763038f1..b3e2319e 100644 --- a/agent-ovs/lib/include/opflexagent/Agent.h +++ b/agent-ovs/lib/include/opflexagent/Agent.h @@ -422,6 +422,7 @@ typedef opflex::ofcore::OFConstants::OpflexElementMode opflex_elem_t; bool behaviorL34FlowsWithoutSubnet; LogParams logParams; /* Persistent policy from disk */ + bool startupPolicyEnabled; boost::optional opflexPolicyFile; }; diff --git a/docker/travis/launch-opflexagent.sh b/docker/travis/launch-opflexagent.sh index 0a4624c3..f08a8115 100755 --- a/docker/travis/launch-opflexagent.sh +++ b/docker/travis/launch-opflexagent.sh @@ -23,6 +23,7 @@ if [ -w ${PREFIX} ]; then mkdir -p ${VARDIR}/lib/opflex-agent-ovs/reboot-conf.d mkdir -p ${VARDIR}/lib/opflex-agent-ovs/droplog mkdir -p ${VARDIR}/lib/opflex-agent-ovs/faults + mkdir -p ${VARDIR}/lib/opflex-agent-ovs/startup fi if [ -d ${OPFLEXAGENT_CONF_PATH} ]; then diff --git a/libopflex/engine/Processor.cpp b/libopflex/engine/Processor.cpp index 1848067a..9995387a 100644 --- a/libopflex/engine/Processor.cpp +++ b/libopflex/engine/Processor.cpp @@ -274,6 +274,8 @@ void Processor::sendToRole(const item& i, uint64_t& newexp, } bool Processor::shouldResolveLocal() { + if (!startupPolicyEnabled) return false; + uint64_t curtime = now(proc_loop); // check >= for duration 0 because now wont update till next uv_loop if (!opflexPolicyFile @@ -698,7 +700,14 @@ bool Processor::waitForPendingItems(uint32_t& wait) { void Processor::setStartupPolicy(boost::optional& file, const modb::ModelMetadata& model, uint64_t& duration, + bool& enabled, bool& resolve_after_connection) { + if (enabled == false) { + LOG(INFO) << "Startup policy disabled"; + return; + } + + startupPolicyEnabled = enabled; opflexPolicyFile = file; if (file) { startupdb.init(model); @@ -733,8 +742,10 @@ void Processor::start(ofcore::OFConstants::OpflexElementMode agent_mode) { LOG(DEBUG) << "Starting OpFlex Processor"; - size_t objs = readStartupPolicy(); - LOG(DEBUG) << "Read " << objs << " objects from startup policy"; + if (startupPolicyEnabled) { + size_t objs = readStartupPolicy(); + LOG(DEBUG) << "Read " << objs << " objects from startup policy"; + } client = &store->getStoreClient("_SYSTEM_"); store->forEachClass(®ister_listeners, this); diff --git a/libopflex/engine/include/opflex/engine/Processor.h b/libopflex/engine/include/opflex/engine/Processor.h index 63ba8041..11dd02a6 100644 --- a/libopflex/engine/include/opflex/engine/Processor.h +++ b/libopflex/engine/include/opflex/engine/Processor.h @@ -272,10 +272,13 @@ class Processor : public internal::AbstractObjectListener, * @param model the model for initializing startupdb * @param duration the amount of time in ms from new * connection to continue using startupdb + * @param enabled if the feature is enabled + * @param resolve_after_connection resolve after leaf connection */ void setStartupPolicy(boost::optional& file, const modb::ModelMetadata& model, uint64_t& duration, + bool& enabled, bool& resolve_after_connection); /** * Enable/Disable reporting of observable changes to registered observers @@ -592,6 +595,11 @@ class Processor : public internal::AbstractObjectListener, */ volatile int64_t newConnectiontime = 0; + /** + * is startup policy enabled + */ + volatile bool startupPolicyEnabled = false; + /** * local resolves only after a new connection till startupPolicyDuration */ diff --git a/libopflex/include/opflex/ofcore/OFFramework.h b/libopflex/include/opflex/ofcore/OFFramework.h index ce48fdd5..3243cd17 100644 --- a/libopflex/include/opflex/ofcore/OFFramework.h +++ b/libopflex/include/opflex/ofcore/OFFramework.h @@ -765,11 +765,13 @@ class OFFramework : private boost::noncopyable { * @param file startup policy file name or boost::none * @param model the model to use for startupdb * @param duration in ms how long from new connection to use startupdb + * @pararm enabled if the startup policy is enabled * @param resolve_after_connection delay local resolves till connection */ void setStartupPolicy(boost::optional& file, const modb::ModelMetadata& model, uint64_t& duration, + bool& enabled, bool& resolve_after_connection); /** diff --git a/libopflex/ofcore/OFFramework.cpp b/libopflex/ofcore/OFFramework.cpp index 81345b7d..f4c050e2 100644 --- a/libopflex/ofcore/OFFramework.cpp +++ b/libopflex/ofcore/OFFramework.cpp @@ -130,9 +130,10 @@ bool OFFramework::waitForPendingItems(uint32_t& wait) { void OFFramework::setStartupPolicy(boost::optional& file, const modb::ModelMetadata& model, uint64_t& duration, + bool& enabled, bool& resolve_after_connection) { - pimpl->processor.setStartupPolicy(file, model, - duration, resolve_after_connection); + pimpl->processor.setStartupPolicy(file, model, duration, + enabled, resolve_after_connection); } void OFFramework::start() {