Skip to content

Commit

Permalink
Add a system-wide unique storage separated from model-access
Browse files Browse the repository at this point in the history
  • Loading branch information
kyechou committed Nov 21, 2023
1 parent 4ff7b95 commit 6fd3ebd
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 84 deletions.
91 changes: 7 additions & 84 deletions src/model-access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,62 +19,10 @@
#include "process/openflow.hpp"
#include "protocols.hpp"
#include "reachcounts.hpp"
#include "unique-storage.hpp"

#include "model.h"

/* variables for preventing duplicates */

class VariableHist {
public:
std::unordered_set<Candidates *, CandHash, CandEq> candidates_hist;
std::unordered_set<FIB *, FIBHash, FIBEq> fib_hist;
std::unordered_set<Choices *, ChoicesHash, ChoicesEq> path_choices_hist;
std::unordered_set<PacketHistory *, PacketHistoryHash, PacketHistoryEq>
pkt_hist_hist;
std::
unordered_set<OpenflowUpdateState *, OFUpdateStateHash, OFUpdateStateEq>
openflow_update_state_hist;
std::unordered_set<ReachCounts *, ReachCountsHash, ReachCountsEq>
reach_counts_hist;

VariableHist() = default;
~VariableHist() { reset(); }

void reset() {
for (Candidates *candidates : this->candidates_hist) {
delete candidates;
}
this->candidates_hist.clear();

for (FIB *fib : this->fib_hist) {
delete fib;
}
this->fib_hist.clear();

for (Choices *path_choices : this->path_choices_hist) {
delete path_choices;
}
this->path_choices_hist.clear();

for (PacketHistory *pkt_hist : this->pkt_hist_hist) {
delete pkt_hist;
}
this->pkt_hist_hist.clear();

for (OpenflowUpdateState *update_state :
this->openflow_update_state_hist) {
delete update_state;
}
this->openflow_update_state_hist.clear();

for (ReachCounts *reach_counts : this->reach_counts_hist) {
delete reach_counts;
}
this->reach_counts_hist.clear();
}
};

static VariableHist storage;
Model &model = Model::get();

Model::Model() : state(nullptr), network(nullptr), openflow(nullptr) {}
Expand All @@ -101,7 +49,6 @@ void Model::reset() {
state = nullptr;
network = nullptr;
openflow = nullptr;
storage.reset();
}

void Model::print_conn_states() const {
Expand Down Expand Up @@ -303,11 +250,7 @@ Candidates *Model::get_candidates() const {

Candidates *Model::set_candidates(Candidates &&candidates) const {
Candidates *new_candidates = new Candidates(std::move(candidates));
auto res = storage.candidates_hist.insert(new_candidates);
if (!res.second) {
delete new_candidates;
new_candidates = *(res.first);
}
new_candidates = storage.store_candidates(new_candidates);
memcpy(state->conn_state[state->conn].candidates, &new_candidates,
sizeof(Candidates *));
return new_candidates;
Expand All @@ -326,11 +269,7 @@ FIB *Model::get_fib() const {

FIB *Model::set_fib(FIB &&fib) const {
FIB *new_fib = new FIB(std::move(fib));
auto res = storage.fib_hist.insert(new_fib);
if (!res.second) {
delete new_fib;
new_fib = *(res.first);
}
new_fib = storage.store_fib(new_fib);
memcpy(state->conn_state[state->conn].fib, &new_fib, sizeof(FIB *));
return new_fib;
}
Expand Down Expand Up @@ -362,11 +301,7 @@ Choices *Model::get_path_choices() const {

Choices *Model::set_path_choices(Choices &&path_choices) const {
Choices *new_path_choices = new Choices(std::move(path_choices));
auto res = storage.path_choices_hist.insert(new_path_choices);
if (!res.second) {
delete new_path_choices;
new_path_choices = *(res.first);
}
new_path_choices = storage.store_choices(new_path_choices);
memcpy(state->conn_state[state->conn].path_choices, &new_path_choices,
sizeof(Choices *));
return new_path_choices;
Expand Down Expand Up @@ -420,11 +355,7 @@ PacketHistory *Model::get_pkt_hist() const {

PacketHistory *Model::set_pkt_hist(PacketHistory &&pkt_hist) const {
PacketHistory *new_pkt_hist = new PacketHistory(std::move(pkt_hist));
auto res = storage.pkt_hist_hist.insert(new_pkt_hist);
if (!res.second) {
delete new_pkt_hist;
new_pkt_hist = *(res.first);
}
new_pkt_hist = storage.store_pkt_hist(new_pkt_hist);
memcpy(state->pkt_hist, &new_pkt_hist, sizeof(PacketHistory *));
return new_pkt_hist;
}
Expand All @@ -440,11 +371,7 @@ OpenflowUpdateState *
Model::set_openflow_update_state(OpenflowUpdateState &&update_state) const {
OpenflowUpdateState *new_state =
new OpenflowUpdateState(std::move(update_state));
auto res = storage.openflow_update_state_hist.insert(new_state);
if (!res.second) {
delete new_state;
new_state = *(res.first);
}
new_state = storage.store_of_update_state(new_state);
memcpy(state->openflow_update_state, &new_state,
sizeof(OpenflowUpdateState *));
return new_state;
Expand Down Expand Up @@ -474,11 +401,7 @@ ReachCounts *Model::get_reach_counts() const {

ReachCounts *Model::set_reach_counts(ReachCounts &&reach_counts) const {
ReachCounts *new_reach_counts = new ReachCounts(std::move(reach_counts));
auto res = storage.reach_counts_hist.insert(new_reach_counts);
if (!res.second) {
delete new_reach_counts;
new_reach_counts = *(res.first);
}
new_reach_counts = storage.store_reach_counts(new_reach_counts);
memcpy(state->reach_counts, &new_reach_counts, sizeof(ReachCounts *));
return new_reach_counts;
}
2 changes: 2 additions & 0 deletions src/plankton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "model-access.hpp"
#include "payloadmgr.hpp"
#include "stats.hpp"
#include "unique-storage.hpp"

using namespace std;
namespace fs = std::filesystem;
Expand Down Expand Up @@ -118,6 +119,7 @@ void Plankton::reset(bool destruct) {
DropTrace::get().teardown();
PayloadMgr::get().reset();
model.reset();
storage.reset();
drop = nullptr;
_STATS_RESET();
}
Expand Down
128 changes: 128 additions & 0 deletions src/unique-storage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include "unique-storage.hpp"

using namespace std;

UniqueStorage &storage = UniqueStorage::get();

UniqueStorage &UniqueStorage::get() {
static UniqueStorage instance;
return instance;
}

void UniqueStorage::reset() {
for (Candidates *candidates : this->candidates_store) {
delete candidates;
}
this->candidates_store.clear();

for (FIB *fib : this->fib_store) {
delete fib;
}
this->fib_store.clear();

for (Choices *path_choices : this->choices_store) {
delete path_choices;
}
this->choices_store.clear();

for (PacketHistory *pkt_hist : this->pkt_hist_store) {
delete pkt_hist;
}
this->pkt_hist_store.clear();

for (OpenflowUpdateState *update_state :
this->openflow_update_state_store) {
delete update_state;
}
this->openflow_update_state_store.clear();

for (ReachCounts *reach_counts : this->reach_counts_store) {
delete reach_counts;
}
this->reach_counts_store.clear();

for (InjectionResult *result : this->injection_result_store) {
delete result;
}
this->injection_result_store.clear();

for (InjectionResults *results : this->injection_results_store) {
delete results;
}
this->injection_results_store.clear();
}

Candidates *UniqueStorage::store_candidates(Candidates *candidates) {
auto res = storage.candidates_store.insert(candidates);
if (!res.second) {
delete candidates;
candidates = *(res.first);
}
return candidates;
}

FIB *UniqueStorage::store_fib(FIB *fib) {
auto res = storage.fib_store.insert(fib);
if (!res.second) {
delete fib;
fib = *(res.first);
}
return fib;
}

Choices *UniqueStorage::store_choices(Choices *choices) {
auto res = storage.choices_store.insert(choices);
if (!res.second) {
delete choices;
choices = *(res.first);
}
return choices;
}

PacketHistory *UniqueStorage::store_pkt_hist(PacketHistory *pkt_hist) {
auto res = storage.pkt_hist_store.insert(pkt_hist);
if (!res.second) {
delete pkt_hist;
pkt_hist = *(res.first);
}
return pkt_hist;
}

OpenflowUpdateState *
UniqueStorage::store_of_update_state(OpenflowUpdateState *update_state) {
auto res = storage.openflow_update_state_store.insert(update_state);
if (!res.second) {
delete update_state;
update_state = *(res.first);
}
return update_state;
}

ReachCounts *UniqueStorage::store_reach_counts(ReachCounts *reach_counts) {
auto res = storage.reach_counts_store.insert(reach_counts);
if (!res.second) {
delete reach_counts;
reach_counts = *(res.first);
}
return reach_counts;
}

InjectionResult *
UniqueStorage::store_injection_result(InjectionResult *result) {
auto res = storage.injection_result_store.insert(result);
if (!res.second) {
delete result;
result = *(res.first);
}
return result;
}

InjectionResults *
UniqueStorage::store_injection_results(InjectionResults *results) {
auto res = storage.injection_results_store.insert(results);
if (!res.second) {
delete results;
results = *(res.first);
}
return results;
}
56 changes: 56 additions & 0 deletions src/unique-storage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once

#include <unordered_set>

#include "candidates.hpp"
#include "choices.hpp"
#include "fib.hpp"
#include "injection-result.hpp"
#include "pkt-hist.hpp"
#include "process/openflow.hpp"
#include "reachcounts.hpp"

class UniqueStorage {
private:
UniqueStorage() = default;
friend class Plankton;
void reset();

// Variables of unique storage for preventing duplicates
std::unordered_set<Candidates *, CandHash, CandEq> candidates_store;
std::unordered_set<FIB *, FIBHash, FIBEq> fib_store;
std::unordered_set<Choices *, ChoicesHash, ChoicesEq> choices_store;
std::unordered_set<PacketHistory *, PacketHistoryHash, PacketHistoryEq>
pkt_hist_store;
std::
unordered_set<OpenflowUpdateState *, OFUpdateStateHash, OFUpdateStateEq>
openflow_update_state_store;
std::unordered_set<ReachCounts *, ReachCountsHash, ReachCountsEq>
reach_counts_store;
std::
unordered_set<InjectionResult *, InjectionResultHash, InjectionResultEq>
injection_result_store;
std::unordered_set<InjectionResults *,
InjectionResultsHash,
InjectionResultsEq>
injection_results_store;

public:
// Disable the copy constructor and the copy assignment operator
UniqueStorage(const UniqueStorage &) = delete;
UniqueStorage &operator=(const UniqueStorage &) = delete;
~UniqueStorage() { reset(); }

static UniqueStorage &get();

Candidates *store_candidates(Candidates *);
FIB *store_fib(FIB *);
Choices *store_choices(Choices *);
PacketHistory *store_pkt_hist(PacketHistory *);
OpenflowUpdateState *store_of_update_state(OpenflowUpdateState *);
ReachCounts *store_reach_counts(ReachCounts *);
InjectionResult *store_injection_result(InjectionResult *);
InjectionResults *store_injection_results(InjectionResults *);
};

extern UniqueStorage &storage;

0 comments on commit 6fd3ebd

Please sign in to comment.