Skip to content

Commit

Permalink
Add interface for finding gate hooks by class name
Browse files Browse the repository at this point in the history
  • Loading branch information
shinae-woo committed Jul 25, 2018
1 parent 22d987a commit 6fa1d75
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
7 changes: 3 additions & 4 deletions core/bessctl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -195,7 +195,7 @@ static int collect_igates(Module* m, GetModuleInfoResponse* response) {

GetModuleInfoResponse_IGate* igate = response->add_igates();

Track* t = reinterpret_cast<Track*>(g->FindHook(Track::kName));
Track* t = reinterpret_cast<Track*>(g->FindHookByClass(Track::kName));

if (t) {
igate->set_cnt(t->cnt());
Expand Down Expand Up @@ -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<Track*>(g->FindHook(Track::kName));
Track* t = reinterpret_cast<Track*>(g->FindHookByClass(Track::kName));
if (t) {
ogate->set_cnt(t->cnt());
ogate->set_pkts(t->pkts());
Expand Down Expand Up @@ -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()) {
Expand Down
27 changes: 25 additions & 2 deletions core/gate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "gate.h"
#include "gate_hooks/track.h"
#include "utils/format.h"

#include <algorithm>
#include <map>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions core/gate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 6fa1d75

Please sign in to comment.