From 6fa1d75cc271e6a09fc5225999974f6b90bcc933 Mon Sep 17 00:00:00 2001 From: Shinae Woo Date: Mon, 23 Jul 2018 06:13:01 -0700 Subject: [PATCH] Add interface for finding gate hooks by class name --- core/bessctl.cc | 7 +++---- core/gate.cc | 27 +++++++++++++++++++++++++-- core/gate.h | 4 ++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/core/bessctl.cc b/core/bessctl.cc index 8e39e65a3..5b8d3d832 100644 --- a/core/bessctl.cc +++ b/core/bessctl.cc @@ -127,7 +127,7 @@ static Status enable_hook_for_module(ConfigureGateHookResponse* response, return Status::OK; } - //FIXME This operation is not all or nothing + //XXX this codes is not all or nothing if a gatehook command are failed if (is_igate) { for (auto& gate : m->igates()) { if (!gate) { @@ -195,7 +195,7 @@ static int collect_igates(Module* m, GetModuleInfoResponse* response) { GetModuleInfoResponse_IGate* igate = response->add_igates(); - Track* t = reinterpret_cast(g->FindHook(Track::kName)); + Track* t = reinterpret_cast(g->FindHookByClass(Track::kName)); if (t) { igate->set_cnt(t->cnt()); @@ -231,7 +231,7 @@ static int collect_ogates(Module* m, GetModuleInfoResponse* response) { GetModuleInfoResponse_OGate* ogate = response->add_ogates(); ogate->set_ogate(g->gate_idx()); - Track* t = reinterpret_cast(g->FindHook(Track::kName)); + Track* t = reinterpret_cast(g->FindHookByClass(Track::kName)); if (t) { ogate->set_cnt(t->cnt()); ogate->set_pkts(t->pkts()); @@ -1498,7 +1498,6 @@ class BESSControlImpl final : public BESSControl::Service { request->hook().class_name().c_str()); } - //XXX this codes isf not all or nothing if a gatehook command are failed if (request->hook().module_name().length() == 0) { // Install this hook on all modules for (const auto& it : ModuleGraph::GetAllModules()) { diff --git a/core/gate.cc b/core/gate.cc index a25a1a005..6f053b412 100644 --- a/core/gate.cc +++ b/core/gate.cc @@ -30,6 +30,7 @@ #include "gate.h" #include "gate_hooks/track.h" +#include "utils/format.h" #include #include @@ -125,9 +126,10 @@ GateHook *Gate::CreateGateHook(const GateHookBuilder *builder, Gate *gate, int Gate::AddHook(GateHook *hook, pb_error_t *error) { for (const auto &h : hooks_) { if (h->name() == hook->name()) { - *error = pb_errno(EEXIST); + error->set_code(EEXIST); + error->set_errmsg("Fail to Add Hook"); + return -1; } - return -1; } hooks_.push_back(hook); @@ -160,6 +162,27 @@ void Gate::RemoveHook(const std::string &name) { } } +GateHook *Gate::FindHookByClass(const std::string &class_name) { + for (const auto &hook : hooks_) { + if (hook->class_name() == class_name) { + return hook; + } + } + return nullptr; +} + +void Gate::RemoveHookByClass(const std::string &class_name) { + for (auto it = hooks_.begin(); it != hooks_.end(); ++it) { + GateHook *hook = *it; + if (hook->class_name() == class_name) { + delete hook; + hooks_.erase(it); + return; + } + } +} + + // TODO(torek): combine (template) with ModuleBuilder::RunCommand CommandResponse GateHookBuilder::RunCommand( GateHook *hook, const std::string &user_cmd, diff --git a/core/gate.h b/core/gate.h index 64fa7931a..40b82e7e8 100644 --- a/core/gate.h +++ b/core/gate.h @@ -219,6 +219,10 @@ class Gate { void RemoveHook(const std::string &name); + GateHook *FindHookByClass(const std::string &name); + + void RemoveHookByClass(const std::string &name); + void ClearHooks(); protected: