summaryrefslogtreecommitdiffhomepage
path: root/server/peer.go
diff options
context:
space:
mode:
Diffstat (limited to 'server/peer.go')
-rw-r--r--server/peer.go114
1 files changed, 74 insertions, 40 deletions
diff --git a/server/peer.go b/server/peer.go
index 8c3b2426..2e97ccee 100644
--- a/server/peer.go
+++ b/server/peer.go
@@ -41,6 +41,8 @@ type Peer struct {
adjRib *table.AdjRib
peerInfo *table.PeerInfo
outgoing chan *bgp.BGPMessage
+ distPolicies []*policy.Policy
+ defaultDistributePolicy config.DefaultPolicyType
}
func NewPeer(g config.Global, config config.Neighbor) *Peer {
@@ -170,8 +172,10 @@ func (peer *Peer) handleBGPmessage(m *bgp.BGPMessage) ([]*table.Path, bool, []*b
break
}
table.UpdatePathAttrs4ByteAs(body)
- pathList = table.ProcessMessage(m, peer.peerInfo)
- peer.adjRib.UpdateIn(pathList)
+ originalPaths := table.ProcessMessage(m, peer.peerInfo)
+ peer.adjRib.UpdateIn(originalPaths)
+ // apply distribute filter before propagate
+ pathList = applyPolicies(peer, nil, POLICY_DIRECTION_DISTRIBUTE, originalPaths)
}
return pathList, update, bgpMsgList
}
@@ -298,6 +302,35 @@ func (peer *Peer) ToApiStruct() *api.Peer {
}
}
+func (peer *Peer) setPolicy(policyMap map[string]*policy.Policy) {
+ // configure distribute policy
+ policyConfig := peer.config.ApplyPolicy
+ distPolicies := make([]*policy.Policy, 0)
+ for _, policyName := range policyConfig.DistributePolicies {
+ log.WithFields(log.Fields{
+ "Topic": "Peer",
+ "Key": peer.config.NeighborAddress,
+ "PolicyName": policyName,
+ }).Info("distribute policy installed")
+ if pol, ok := policyMap[policyName]; ok {
+ log.Debug("distribute policy : ", pol)
+ distPolicies = append(distPolicies, pol)
+ }
+ }
+ peer.distPolicies = distPolicies
+ peer.defaultDistributePolicy = policyConfig.DefaultDistributePolicy
+
+}
+
+
+func (peer *Peer) applyPolicies(original table.Path) (bool, table.Path) {
+ policies := peer.distPolicies
+ var d Direction = POLICY_DIRECTION_DISTRIBUTE
+
+ return applyPolicy("Peer", peer.config.NeighborAddress.String(), d, policies, original)
+}
+
+
type LocalRib struct {
rib *table.TableManager
importPolicies []*policy.Policy
@@ -377,43 +410,44 @@ func (loc *LocalRib) setPolicy(peer *Peer, policyMap map[string]*policy.Policy)
// modified path.
// If action of the policy is 'reject', return nil
//
-func (loc *LocalRib) applyPolicies(isExport bool, original *table.Path) (bool, *table.Path) {
-
- var applied bool = true
+func (loc *LocalRib) applyPolicies(d Direction, original *table.Path) (bool, *table.Path) {
var policies []*policy.Policy
- var direction string
- if isExport == true {
- policies = loc.exportPolicies
- direction = "export"
- } else {
- policies = loc.importPolicies
- direction = "import"
- }
-
- for _, pol := range policies {
- if result, action, newpath := pol.Apply(original); result {
- log.Debug("newpath: ", newpath)
- if action == policy.ROUTE_TYPE_REJECT {
- log.WithFields(log.Fields{
- "Topic": "Loc",
- "Key": loc.OwnerName(),
- "NRLI": original.GetNlri(),
- "Dir": direction,
- }).Debug("path was rejected")
- // return applied, nil, this means path was rejected
- return applied, nil
- } else {
- // return applied, new path
- return applied, newpath
- }
- }
- }
- log.WithFields(log.Fields{
- "Topic": "Loc",
- "Key": loc.OwnerName(),
- "Len": len(policies),
- "NRLI": original,
- "Dir": direction,
- }).Debug("no policy applied")
- return !applied, original
+ switch(d) {
+ case POLICY_DIRECTION_EXPORT:
+ policies = loc.exportPolicies
+ case POLICY_DIRECTION_IMPORT:
+ policies = loc.importPolicies
+ }
+ return applyPolicy("Loc", loc.OwnerName(), d, policies, original)
}
+
+func applyPolicy(component, owner string, d Direction, policies []*policy.Policy, original table.Path) (bool, table.Path){
+ var applied bool = true
+ for _, pol := range policies {
+ if result, action, newpath := pol.Apply(original); result {
+ log.Debug("newpath: ", newpath)
+ if action == policy.ROUTE_TYPE_REJECT {
+ log.WithFields(log.Fields{
+ "Topic": component,
+ "Key": owner,
+ "NRLI": original.GetNlri(),
+ "Dir": d,
+ }).Debug("path was rejected")
+ // return applied, nil, this means path was rejected
+ return applied, nil
+ } else {
+ // return applied, new path
+ return applied, newpath
+ }
+ }
+ }
+
+ log.WithFields(log.Fields{
+ "Topic": component,
+ "Key": owner,
+ "Len": len(policies),
+ "NRLI": original,
+ "Dir": d,
+ }).Debug("no policy applied")
+ return !applied, original
+} \ No newline at end of file