Skip to content

Commit

Permalink
Routing policy operational code changes
Browse files Browse the repository at this point in the history
TODOs:
    1. handle update of policy
    2. Handle update of policies on routing instance
    3. UT coverage
    4. Comments and formatting

Routing policy Unit test cases

Bug fix for inet6 prefix match

Change-Id: I83de4a24f52c9781d61f4325246b10e115ad8af6
Related-bug: #1500698
  • Loading branch information
bailkeri committed Dec 5, 2015
1 parent 2560515 commit 8e1ccb6
Show file tree
Hide file tree
Showing 51 changed files with 3,145 additions and 72 deletions.
5 changes: 4 additions & 1 deletion src/bgp/bgp_common.h
Expand Up @@ -4,8 +4,11 @@

#ifndef ctrlplane_bgp_common_h
#define ctrlplane_bgp_common_h

#include <boost/intrusive_ptr.hpp>
typedef uint16_t as_t;
typedef uint32_t as4_t;

class RoutingPolicy;
typedef boost::intrusive_ptr<RoutingPolicy> RoutingPolicyPtr;

#endif
10 changes: 5 additions & 5 deletions src/bgp/bgp_config.h
Expand Up @@ -288,7 +288,7 @@ struct PrefixMatch {
std::string prefix_match_type;
};

struct RoutingPolicyMatch {
struct RoutingPolicyMatchConfig {
std::string community_match;
PrefixMatch prefix_match;
};
Expand All @@ -300,7 +300,7 @@ struct ActionUpdate {
uint32_t local_pref;
};

struct RoutingPolicyAction {
struct RoutingPolicyActionConfig {
enum ActionType {
ACCEPT,
REJECT,
Expand All @@ -311,8 +311,8 @@ struct RoutingPolicyAction {
};

struct RoutingPolicyTerm {
RoutingPolicyMatch match;
RoutingPolicyAction action;
RoutingPolicyMatchConfig match;
RoutingPolicyActionConfig action;
};

// Route Policy configuration.
Expand All @@ -338,7 +338,7 @@ class BgpRoutingPolicyConfig {
};

struct RoutingPolicyAttachInfo {
std::string sequence_;
double sequence_;
std::string routing_policy_;
};

Expand Down
36 changes: 25 additions & 11 deletions src/bgp/bgp_config_ifmap.cc
Expand Up @@ -6,6 +6,7 @@

#include <boost/foreach.hpp>

#include "base/string_util.h"
#include "base/task_annotations.h"
#include "bgp/bgp_config_listener.h"
#include "bgp/bgp_log.h"
Expand Down Expand Up @@ -713,21 +714,26 @@ static void GetConnectionExportTargets(DBGraph *graph, IFMapNode *src_node,
// IFMapNode represents the routing-instance-routing-policy. We traverse to
// graph edges and look for routing-policy adjacency
//
static void GetRoutingInstanceRoutingPolicy(DBGraph *graph, IFMapNode *node,
RoutingPolicyAttachInfo *routing_policy) {
static bool GetRoutingInstanceRoutingPolicy(DBGraph *graph, IFMapNode *node,
RoutingPolicyAttachInfo *ri_rp_link) {
std::string sequence;
const autogen::RoutingInstanceRoutingPolicy *policy =
static_cast<autogen::RoutingInstanceRoutingPolicy *>(node->GetObject());
const autogen::RoutingPolicyType &attach_info = policy->data();
routing_policy->sequence_ = attach_info.sequence;

if (!stringToInteger(attach_info.sequence, ri_rp_link->sequence_)) {
return false;
}

for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
iter != node->end(graph); ++iter) {
IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
if (strcmp(adj->table()->Typename(), "routing-policy") == 0) {
routing_policy->routing_policy_ = adj->name();
return;
ri_rp_link->routing_policy_ = adj->name();
return true;
}
}
return false;
}

//
Expand Down Expand Up @@ -904,6 +910,11 @@ void BgpIfmapConfigManager::ProcessRoutingPolicyLink(const BgpConfigDelta &delta

ri_rp_link->Update(this, ri_rp_link_cfg);
}

static bool CompareRoutingPolicyOrder(const RoutingPolicyAttachInfo &lhs,
const RoutingPolicyAttachInfo &rhs) {
return (lhs.sequence_ < rhs.sequence_);
}
//
// Update BgpIfmapInstanceConfig based on a new autogen::RoutingInstance object.
//
Expand Down Expand Up @@ -948,8 +959,9 @@ void BgpIfmapInstanceConfig::Update(BgpIfmapConfigManager *manager,
} else if (strcmp(adj->table()->Typename(),
"routing-instance-routing-policy") == 0) {
RoutingPolicyAttachInfo policy_info;
GetRoutingInstanceRoutingPolicy(graph, adj, &policy_info);
policy_list.push_back(policy_info);
if (GetRoutingInstanceRoutingPolicy(graph, adj, &policy_info)) {
policy_list.push_back(policy_info);
}
} else if (strcmp(adj->table()->Typename(), "connection") == 0) {
vector<string> target_list;
GetConnectionExportTargets(graph, node, name_, adj, &target_list);
Expand All @@ -967,6 +979,8 @@ void BgpIfmapInstanceConfig::Update(BgpIfmapConfigManager *manager,

data_.set_import_list(import_list);
data_.set_export_list(export_list);

std::sort(policy_list.begin(), policy_list.end(), CompareRoutingPolicyOrder);
data_.swap_routing_policy_list(&policy_list);

if (config) {
Expand Down Expand Up @@ -1525,11 +1539,11 @@ static void BuildPolicyTerm(autogen::PolicyTerm cfg_term,
term->action.update.community_set.push_back(community);
}
term->action.update.local_pref = cfg_term.then.update.local_pref;
term->action.action = RoutingPolicyAction::ACCEPT;
term->action.action = RoutingPolicyActionConfig::NEXT_TERM;
if (strcmp(cfg_term.then.action.c_str(), "reject") == 0) {
term->action.action = RoutingPolicyAction::REJECT;
} else if (strcmp(cfg_term.then.action.c_str(), "next") == 0) {
term->action.action = RoutingPolicyAction::NEXT_TERM;
term->action.action = RoutingPolicyActionConfig::REJECT;
} else if (strcmp(cfg_term.then.action.c_str(), "accept") == 0) {
term->action.action = RoutingPolicyActionConfig::ACCEPT;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/bgp/bgp_config_listener.cc
Expand Up @@ -62,7 +62,8 @@ void BgpConfigListener::DependencyTrackerInit() {

ReactionMap routing_policy_assoc_react = map_list_of<string, PropagateList>
("self", list_of("routing-instance-routing-policy"))
("routing-instance-routing-policy", list_of("routing-instance-routing-policy"));
("routing-instance-routing-policy",
list_of("routing-instance-routing-policy"));
policy->insert(make_pair("routing-instance-routing-policy",
routing_policy_assoc_react));

Expand Down
1 change: 1 addition & 0 deletions src/bgp/bgp_factory.cc
Expand Up @@ -34,6 +34,7 @@ FACTORY_STATIC_REGISTER(BgpObjectFactory, RoutingInstanceMgr,
RoutingInstanceMgr);

#include "bgp/routing-policy/routing_policy.h"
FACTORY_STATIC_REGISTER(BgpObjectFactory, RoutingPolicy, RoutingPolicy);
FACTORY_STATIC_REGISTER(BgpObjectFactory, RoutingPolicyMgr, RoutingPolicyMgr);

#include "bgp/routing-instance/rtarget_group_mgr.h"
Expand Down
5 changes: 5 additions & 0 deletions src/bgp/bgp_factory.h
Expand Up @@ -16,6 +16,7 @@ class BgpConfigListener;
class BgpConfigManager;
class BgpExport;
class BgpInstanceConfig;
class BgpRoutingPolicyConfig;
class BgpMessageBuilder;
class BgpNeighborConfig;
class BgpPeer;
Expand All @@ -37,6 +38,7 @@ class RibOut;
class RibOutUpdates;
class RoutingInstance;
class RoutingInstanceMgr;
class RoutingPolicy;
class RoutingPolicyMgr;
class RTargetGroupMgr;
class SchedulingGroup;
Expand All @@ -63,6 +65,9 @@ class BgpObjectFactory : public Factory<BgpObjectFactory> {
FACTORY_TYPE_N4(BgpObjectFactory, RoutingInstance,
std::string, BgpServer *, RoutingInstanceMgr *,
const BgpInstanceConfig *);
FACTORY_TYPE_N4(BgpObjectFactory, RoutingPolicy,
std::string, BgpServer *, RoutingPolicyMgr *,
const BgpRoutingPolicyConfig *);
FACTORY_TYPE_N0(BgpObjectFactory, BgpMessageBuilder);
FACTORY_TYPE_N0(BgpObjectFactory, BgpXmppMessageBuilder);

Expand Down
29 changes: 27 additions & 2 deletions src/bgp/bgp_path.h
Expand Up @@ -24,6 +24,7 @@ class BgpPath : public Path {
NoTunnelEncap = 1 << 3,
OriginatorIdLooped = 1 << 4,
ResolveNexthop = 1 << 5,
RoutingPolicyReject = 1 << 6,
};

// Ordered in the ascending order of path preference
Expand All @@ -37,7 +38,8 @@ class BgpPath : public Path {
};

static const uint32_t INFEASIBLE_MASK = (AsPathLooped |
NoNeighborAs | NoTunnelEncap | OriginatorIdLooped | ResolveNexthop);
NoNeighborAs | NoTunnelEncap | OriginatorIdLooped | ResolveNexthop |
RoutingPolicyReject);

static std::string PathIdString(uint32_t path_id);
static std::string PathSourceString(PathSource source);
Expand Down Expand Up @@ -80,10 +82,19 @@ class BgpPath : public Path {
peer_->UpdatePrimaryPathCount(count);
}

void SetAttr(const BgpAttrPtr attr, const BgpAttrPtr original_attr) {
attr_ = attr;
original_attr_ = original_attr;
}

const BgpAttr *GetAttr() const {
return attr_.get();
}

const BgpAttr *GetOriginalPathAttr() const {
return original_attr_.get();
}

uint32_t GetLabel() const {
return label_;
}
Expand All @@ -109,6 +120,16 @@ class BgpPath : public Path {
return ((flags_ & Stale) != 0);
}

// Mark a path as rejected by Routing policy
void SetPolicyReject() {
flags_ |= RoutingPolicyReject;
}

// Reset a path as active from Routing Policy
void ResetPolicyReject() {
flags_ &= ~RoutingPolicyReject;
}

// Mark a path as stale
void SetStale() {
flags_ |= Stale;
Expand All @@ -133,7 +154,11 @@ class BgpPath : public Path {
const IPeer *peer_;
const uint32_t path_id_;
const PathSource source_;
const BgpAttrPtr attr_;
// Attribute for the BgpPath. If routing policy updates the path attribute,
// this member contains the attribute after policy update
BgpAttrPtr attr_;
// Original path attribute before applying routing policy
BgpAttrPtr original_attr_;
uint32_t flags_;
uint32_t label_;
};
Expand Down
6 changes: 5 additions & 1 deletion src/bgp/bgp_route.cc
Expand Up @@ -43,12 +43,16 @@ void BgpRoute::InsertPath(BgpPath *path) {
assert(!IsDeleted());
const Path *prev_front = front();

BgpTable *table = static_cast<BgpTable *>(get_table());
if (table) {
RoutingInstance *rtinstance = table->routing_instance();
rtinstance->ProcessRoutingPolicy(this, path);
}
insert(path);

Sort(&BgpTable::PathSelection, prev_front);

// Update counters.
BgpTable *table = static_cast<BgpTable *>(get_table());
if (table) table->UpdatePathCount(path, +1);
path->UpdatePeerRefCount(+1);
}
Expand Down
12 changes: 7 additions & 5 deletions src/bgp/bgp_server.cc
Expand Up @@ -189,12 +189,14 @@ class BgpServer::ConfigUpdater {
}

void ProcessRoutingPolicyConfig(const BgpRoutingPolicyConfig *policy_config,
BgpConfigManager::EventType event) {
if (event == BgpConfigManager::CFG_ADD ||
event == BgpConfigManager::CFG_CHANGE) {
return;
BgpConfigManager::EventType event) {
RoutingPolicyMgr *mgr = server_->routing_policy_mgr();
if (event == BgpConfigManager::CFG_ADD) {
mgr->CreateRoutingPolicy(policy_config);
} else if (event == BgpConfigManager::CFG_CHANGE) {
mgr->UpdateRoutingPolicy(policy_config);
} else if (event == BgpConfigManager::CFG_DELETE) {
return;
mgr->DeleteRoutingPolicy(policy_config->name());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/bgp/bgp_server.h
Expand Up @@ -85,8 +85,8 @@ class BgpServer {
const RoutingInstanceMgr *routing_instance_mgr() const {
return inst_mgr_.get();
}
RoutingPolicyMgr *route_policy_mgr() { return policy_mgr_.get(); }
const RoutingPolicyMgr *route_policy_mgr() const {
RoutingPolicyMgr *routing_policy_mgr() { return policy_mgr_.get(); }
const RoutingPolicyMgr *routing_policy_mgr() const {
return policy_mgr_.get();
}
RTargetGroupMgr *rtarget_group_mgr() { return rtarget_group_mgr_.get(); }
Expand Down
63 changes: 63 additions & 0 deletions src/bgp/community.cc
Expand Up @@ -64,6 +64,30 @@ void Community::Append(uint32_t value) {
sort(communities_.begin(), communities_.end());
}

void Community::Append(const std::vector<uint32_t> &communities) {
BOOST_FOREACH(uint32_t community, communities) {
communities_.push_back(community);
}
sort(communities_.begin(), communities_.end());
vector<uint32_t>::iterator it =
unique(communities_.begin(), communities_.end());
communities_.erase(it, communities_.end());
}

void Community::Set(const std::vector<uint32_t> &communities) {
communities_.clear();
BOOST_FOREACH(uint32_t community, communities) {
communities_.push_back(community);
}
}

void Community::Remove(const std::vector<uint32_t> &communities) {
BOOST_FOREACH(uint32_t community, communities) {
communities_.erase(
std::remove(communities_.begin(), communities_.end(), community),
communities_.end());
}
}
void Community::Remove() {
comm_db_->Delete(this);
}
Expand Down Expand Up @@ -99,6 +123,45 @@ CommunityPtr CommunityDB::AppendAndLocate(const Community *src,
return Locate(clone);
}

CommunityPtr CommunityDB::AppendAndLocate(const Community *src,
const std::vector<uint32_t> &value) {
Community *clone;
if (src) {
clone = new Community(*src);
} else {
clone = new Community(this);
}

clone->Append(value);
return Locate(clone);
}

CommunityPtr CommunityDB::SetAndLocate(const Community *src,
const std::vector<uint32_t> &value) {
Community *clone;
if (src) {
clone = new Community(*src);
} else {
clone = new Community(this);
}

clone->Set(value);
return Locate(clone);
}

CommunityPtr CommunityDB::RemoveAndLocate(const Community *src,
const std::vector<uint32_t> &value) {
Community *clone;
if (src) {
clone = new Community(*src);
} else {
clone = new Community(this);
}

clone->Remove(value);
return Locate(clone);
}

string ExtCommunitySpec::ToString() const {
char repr[80];
snprintf(repr, sizeof(repr), "ExtCommunity <code: %d, flags: %02x>:%d",
Expand Down

0 comments on commit 8e1ccb6

Please sign in to comment.