Skip to content

Commit

Permalink
assigning priorities to classifiers and UTs for the same
Browse files Browse the repository at this point in the history
  • Loading branch information
bashokba committed Sep 30, 2020
1 parent 4dfc47a commit 4bad936
Show file tree
Hide file tree
Showing 4 changed files with 751 additions and 13 deletions.
85 changes: 75 additions & 10 deletions agent-ovs/lib/PolicyManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include <modelgbp/gbp/DirectionEnumT.hpp>
#include <modelgbp/gbp/HashingAlgorithmEnumT.hpp>
#include <opflex/modb/URIBuilder.h>
#include <modelgbp/gbp/ConnTrackEnumT.hpp>
#include <modelgbp/l2/EtherTypeEnumT.hpp>
#include <modelgbp/l4/TcpFlagsEnumT.hpp>

#include <opflexagent/logging.h>
#include <opflexagent/PolicyManager.h>
Expand Down Expand Up @@ -1054,6 +1057,30 @@ void resolveRemoteSubnets(OFFramework& framework,
}
}

void addClsrToMap(const shared_ptr<modelgbp::gbpe::L24Classifier>& currClsr, std::multimap<uint32_t,shared_ptr<modelgbp::gbpe::L24Classifier>>& storeClsr){
using modelgbp::gbpe::L24Classifier;
const shared_ptr<L24Classifier>& nextClsr = *(&currClsr+1);
if (currClsr->getOrder(0) == nextClsr->getOrder(0)) {
if(storeClsr.empty()){
storeClsr.insert({currClsr->getOrder(0),currClsr});
}
auto retClsr = storeClsr.equal_range(currClsr->getOrder(0));
bool foundClsr = 0;
for (auto it = retClsr.first; it != retClsr.second ; ++it){
if (it->second == currClsr)
foundClsr = 1;
}

if (foundClsr) {
storeClsr.insert({nextClsr->getOrder(0),nextClsr});
} else {
storeClsr.insert({currClsr->getOrder(0),currClsr});
storeClsr.insert({nextClsr->getOrder(0),nextClsr});
}
}

}

template <typename Parent, typename Subject, typename Rule>
static bool updatePolicyRules(OFFramework& framework,
const URI& parentURI, bool& notFound,
Expand Down Expand Up @@ -1168,17 +1195,55 @@ static bool updatePolicyRules(OFFramework& framework,
}

uint16_t clsPrio = 0;
for (const shared_ptr<L24Classifier>& c : classifiers) {
newRules.push_back(std::
make_shared<PolicyRule>(dir,
rulePrio - clsPrio,
c, ruleAllow,
remoteSubnets,
ruleRedirect, ruleLog,
destGrpUri));
if (clsPrio < 127)
clsPrio += 1;
std::multimap<uint32_t,shared_ptr<L24Classifier>> storeClsr;
vector<unsigned> storeOrder;
PriorityComparator<shared_ptr<L24Classifier> > classifierPrioComp;
auto &lastClsr = *(--classifiers.end());

for (const shared_ptr<L24Classifier>& currClsr : classifiers) {
if (&currClsr != &lastClsr)
addClsrToMap(currClsr,storeClsr);
}

for (const shared_ptr<L24Classifier>& currClsr : classifiers) {
if (storeClsr.find(currClsr->getOrder(0)) != storeClsr.end()) {
if (find(storeOrder.begin(), storeOrder.end(),currClsr->getOrder(0)) == storeOrder.end()){
auto ret = storeClsr.equal_range(currClsr->getOrder(0));
vector<shared_ptr<L24Classifier> > sortedClsr;
for (auto it=ret.first; it!=ret.second; ++it) {
sortedClsr.push_back(it->second);
}
stable_sort(sortedClsr.begin(), sortedClsr.end(), classifierPrioComp);
for (auto c : sortedClsr) {
newRules.push_back(std::
make_shared<PolicyRule>(dir,
rulePrio - clsPrio,
c, ruleAllow,
remoteSubnets,
ruleRedirect, ruleLog,
destGrpUri));
if (clsPrio < 127)
clsPrio += 1;
}
storeOrder.push_back(currClsr->getOrder(0));

}
else continue;
}

else {
newRules.push_back(std::
make_shared<PolicyRule>(dir,
rulePrio - clsPrio,
currClsr, ruleAllow,
remoteSubnets,
ruleRedirect, ruleLog,
destGrpUri));
if (clsPrio < 127)
clsPrio += 1;
}

}
if (rulePrio > 128)
rulePrio -= 128;
}
Expand Down
37 changes: 37 additions & 0 deletions agent-ovs/lib/include/opflexagent/PolicyManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,43 @@ struct OrderComparator {
}
};

/**
* Add the classifier objects of same orders to the map in the sequence they came in
*
*/
void addClsrToMap(const std::shared_ptr<modelgbp::gbpe::L24Classifier>& currClsr, std::multimap<uint32_t, std::shared_ptr<modelgbp::gbpe::L24Classifier>>& storeClsr);

/**
* Comparator for sorting classifier objects based on the priorities
* assigned to the members of classifiers
*/

template<typename T>
struct PriorityComparator {

bool operator()(const T& lhs, const T& rhs) {

if (lhs->isTcpFlagsSet() > rhs->isTcpFlagsSet()) { return true; }
if (lhs->isTcpFlagsSet() < rhs->isTcpFlagsSet()) { return false; }

if ((lhs->isDFromPortSet() && lhs->isSFromPortSet()) > (rhs->isDFromPortSet() && rhs->isSFromPortSet() )) { return true; }
if ((lhs->isDFromPortSet() && lhs->isSFromPortSet()) < (rhs->isDFromPortSet() && rhs->isSFromPortSet())) { return false; }

if (lhs->isDFromPortSet() > rhs->isDFromPortSet()) { return true; }
if (lhs->isDFromPortSet() < rhs->isDFromPortSet()) { return false; }

if (lhs->isSFromPortSet() > rhs->isSFromPortSet()) { return true; }
if (lhs->isSFromPortSet() < rhs->isSFromPortSet()) { return false; }

if (lhs->isProtSet() > rhs->isProtSet()) { return true; }
if (lhs->isProtSet() < rhs->isProtSet()) { return false; }

if (lhs->isFragmentFlagsSet() < rhs->isFragmentFlagsSet()) { return true; }
if (lhs->isFragmentFlagsSet() > rhs->isFragmentFlagsSet()) { return false; }

return false;
}
};
} /* namespace opflexagent */

#endif /* OPFLEXAGENT_POLICYMANAGER_H */
Loading

0 comments on commit 4bad936

Please sign in to comment.