diff options
author | Naoto Hanaue <hanaue.naoto@po.ntts.co.jp> | 2015-05-25 12:03:50 +0900 |
---|---|---|
committer | Naoto Hanaue <hanaue.naoto@po.ntts.co.jp> | 2015-05-26 17:31:58 +0900 |
commit | cdf36e2a7d01c4aa713a1c3883d15a7c59964879 (patch) | |
tree | 2317c24811b1f9f0faae8fd44c6471b1a91b09b5 /server | |
parent | fd4f6a91efb81646281ae8175433872627ec8611 (diff) |
cli: add the add/del commands to policy of neighbor
% gobgp -u 10.0.255.1 neighbor 10.0.0.3 policy
DefaultImportPolicy: ACCEPT
DefaultExportPolicy: ACCEPT
ImportPolicies:
PolicyName policy1:
StatementName st0:
Conditions:
PrefixSet: ps2 192.168.20.0/24
NeighborSet: ns0 10.0.0.2
10.0.0.3
AsPathLength: eq 5
MatchOption: ALL
Actions:
ACCEPT
ExportPolicies:
% gobgp -u 10.0.255.1 neighbor 10.0.0.3 policy add export policy0 reject
% gobgp -u 10.0.255.1 neighbor 10.0.0.3 policy del import
% gobgp -u 10.0.255.1 neighbor 10.0.0.3 policy
DefaultImportPolicy: ACCEPT
DefaultExportPolicy: REJECT
ImportPolicies:
ExportPolicies:
PolicyName policy0:
StatementName st0:
Conditions:
PrefixSet: ps0 192.168.0.0/16 16..24
NeighborSet: ns2 10.0.0.4
AsPathLength:
MatchOption: ALL
Actions:
REJECT
Diffstat (limited to 'server')
-rw-r--r-- | server/grpc_server.go | 50 | ||||
-rw-r--r-- | server/peer.go | 46 | ||||
-rw-r--r-- | server/server.go | 68 |
3 files changed, 130 insertions, 34 deletions
diff --git a/server/grpc_server.go b/server/grpc_server.go index f1d040a2..79a3dc4d 100644 --- a/server/grpc_server.go +++ b/server/grpc_server.go @@ -41,6 +41,10 @@ const ( REQ_NEIGHBOR_ENABLE REQ_NEIGHBOR_DISABLE REQ_NEIGHBOR_POLICY + REQ_NEIGHBOR_POLICY_ADD_IMPORT + REQ_NEIGHBOR_POLICY_ADD_EXPORT + REQ_NEIGHBOR_POLICY_DEL_IMPORT + REQ_NEIGHBOR_POLICY_DEL_EXPORT REQ_GLOBAL_RIB REQ_GLOBAL_ADD REQ_GLOBAL_DELETE @@ -293,6 +297,52 @@ func (s *Server) GetNeighborPolicy(ctx context.Context, arg *api.Arguments) (*ap return res.Data.(*api.ApplyPolicy), nil } +func (s *Server) ModNeighborPolicy(stream api.Grpc_ModNeighborPolicyServer) error { + for { + arg, err := stream.Recv() + if err == io.EOF { + return nil + } else if err != nil { + return err + } + + if arg.Resource != api.Resource_POLICY_ROUTEPOLICY { + return fmt.Errorf("unsupported resource: %s", arg.Resource) + } + var rf bgp.RouteFamily + var reqType int + switch arg.Operation { + case api.Operation_ADD: + switch arg.Name { + case "import": + reqType = REQ_NEIGHBOR_POLICY_ADD_IMPORT + case "export": + reqType = REQ_NEIGHBOR_POLICY_ADD_EXPORT + } + case api.Operation_DEL: + switch arg.Name { + case "import": + reqType = REQ_NEIGHBOR_POLICY_DEL_IMPORT + case "export": + reqType = REQ_NEIGHBOR_POLICY_DEL_EXPORT + } + } + req := NewGrpcRequest(reqType, arg.RouterId, rf, arg.ApplyPolicy) + s.bgpServerCh <- req + res := <-req.ResponseCh + if err := res.Err(); err != nil { + log.Debug(err.Error()) + return err + } + err = stream.Send(&api.Error{ + Code: api.Error_SUCCESS, + }) + if err != nil { + return err + } + } +} + func (s *Server) getPolicies(reqType int, arg *api.PolicyArguments, stream interface{}) error { var rf bgp.RouteFamily req := NewGrpcRequest(reqType, "", rf, nil) diff --git a/server/peer.go b/server/peer.go index 02585e11..46db99a9 100644 --- a/server/peer.go +++ b/server/peer.go @@ -132,6 +132,7 @@ func (peer *Peer) setPolicy(policyMap map[string]*policy.Policy) { } } peer.importPolicies = inPolicies + peer.defaultImportPolicy = policyConfig.DefaultImportPolicy // configure export policy outPolicies := make([]*policy.Policy, 0) @@ -147,6 +148,7 @@ func (peer *Peer) setPolicy(policyMap map[string]*policy.Policy) { } } peer.exportPolicies = outPolicies + peer.defaultExportPolicy = policyConfig.DefaultExportPolicy } func (peer *Peer) configuredRFlist() []bgp.RouteFamily { @@ -588,10 +590,8 @@ func (peer *Peer) handleGrpc(grpcReq *GrpcRequest) { } result.Data = err case REQ_NEIGHBOR_POLICY: - result := &GrpcResponse{} resInPolicies := []*api.PolicyDefinition{} resOutPolicies := []*api.PolicyDefinition{} - // Add importpolies that has been set in the configuration file to the list. // However, peer haven't target importpolicy when add PolicyDefinition of name only to the list. conInPolicyNames := peer.peerConfig.ApplyPolicy.ImportPolicies @@ -624,13 +624,13 @@ func (peer *Peer) handleGrpc(grpcReq *GrpcRequest) { resOutPolicies = append(resOutPolicies, &api.PolicyDefinition{PolicyDefinitionName: conOutPolicyName}) } } - defaultInPolicy := "REJECT" - defaultOutPolicy := "REJECT" - if peer.defaultImportPolicy == 0 { - defaultInPolicy = "ACCEPT" + defaultInPolicy := policy.ROUTE_REJECT + defaultOutPolicy := policy.ROUTE_REJECT + if peer.defaultImportPolicy == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE { + defaultInPolicy = policy.ROUTE_ACCEPT } - if peer.defaultExportPolicy == 0 { - defaultOutPolicy = "ACCEPT" + if peer.defaultExportPolicy == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE { + defaultOutPolicy = policy.ROUTE_ACCEPT } result.Data = &api.ApplyPolicy{ DefaultImportPolicy: defaultInPolicy, @@ -638,10 +638,32 @@ func (peer *Peer) handleGrpc(grpcReq *GrpcRequest) { DefaultExportPolicy: defaultOutPolicy, ExportPolicies: resOutPolicies, } - grpcReq.ResponseCh <- result - - close(grpcReq.ResponseCh) - return + case REQ_NEIGHBOR_POLICY_ADD_IMPORT, REQ_NEIGHBOR_POLICY_ADD_EXPORT, REQ_NEIGHBOR_POLICY_DEL_IMPORT, REQ_NEIGHBOR_POLICY_DEL_EXPORT: + data := grpcReq.Data.([]interface{}) + reqApplyPolicy := data[0].(*api.ApplyPolicy) + reqPolicyMap := data[1].(map[string]*policy.Policy) + applyPolicy := &peer.peerConfig.ApplyPolicy + var defInPolicy, defOutPolicy config.DefaultPolicyType + if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_ADD_IMPORT { + if reqApplyPolicy.DefaultImportPolicy != policy.ROUTE_ACCEPT { + defInPolicy = config.DEFAULT_POLICY_TYPE_REJECT_ROUTE + } + peer.peerConfig.ApplyPolicy.DefaultImportPolicy = defInPolicy + applyPolicy.ImportPolicies = policy.PoliciesToString(reqApplyPolicy.ImportPolicies) + } else if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_ADD_EXPORT { + if reqApplyPolicy.DefaultExportPolicy != policy.ROUTE_ACCEPT { + defOutPolicy = config.DEFAULT_POLICY_TYPE_REJECT_ROUTE + } + peer.peerConfig.ApplyPolicy.DefaultExportPolicy = defOutPolicy + applyPolicy.ExportPolicies = policy.PoliciesToString(reqApplyPolicy.ExportPolicies) + } else if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_DEL_IMPORT { + peer.peerConfig.ApplyPolicy.DefaultImportPolicy = config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE + peer.peerConfig.ApplyPolicy.ImportPolicies = make([]string, 0) + } else if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_DEL_EXPORT { + peer.peerConfig.ApplyPolicy.DefaultExportPolicy = config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE + peer.peerConfig.ApplyPolicy.ExportPolicies = make([]string, 0) + } + peer.setPolicy(reqPolicyMap) } grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) diff --git a/server/server.go b/server/server.go index 88ff5a99..47166602 100644 --- a/server/server.go +++ b/server/server.go @@ -325,6 +325,23 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) } + case REQ_NEIGHBOR_POLICY_ADD_IMPORT, REQ_NEIGHBOR_POLICY_ADD_EXPORT, REQ_NEIGHBOR_POLICY_DEL_IMPORT, REQ_NEIGHBOR_POLICY_DEL_EXPORT: + remoteAddr := grpcReq.RemoteAddr + result := &GrpcResponse{} + info, found := server.peerMap[remoteAddr] + if found { + reqApplyPolicy := grpcReq.Data.(*api.ApplyPolicy) + grpcReq.Data = []interface{}{reqApplyPolicy, server.policyMap} + msg := &serverMsg{ + msgType: SRV_MSG_API, + msgData: grpcReq, + } + info.peer.serverMsgCh <- msg + } else { + result.ResponseErr = fmt.Errorf("Neighbor that has %v doesn't exist.", remoteAddr) + grpcReq.ResponseCh <- result + close(grpcReq.ResponseCh) + } case REQ_POLICY_PREFIXES: info := server.routingPolicy.DefinedSets.PrefixSetList result := &GrpcResponse{} @@ -395,16 +412,17 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { // If only name of the PrefixSet is same, delete all of the elements of the PrefixSet. // If the same element PrefixSet, delete the it's element from PrefixSet. idxPrefixSet, idxPrefix := policy.IndexOfPrefixSet(conPrefixSetList, prefixSet) + prefix := prefixSet.PrefixList[0] if idxPrefixSet == -1 { result.ResponseErr = fmt.Errorf("Policy prefix that has %v %v/%v %v doesn't exist.", prefixSet.PrefixSetName, - prefixSet.PrefixList[0].Address, prefixSet.PrefixList[0].Masklength, prefixSet.PrefixList[0].MasklengthRange) + prefix.Address, prefix.Masklength, prefix.MasklengthRange) } else { if idxPrefix == -1 { result.ResponseErr = fmt.Errorf("Policy prefix that has %v %v/%v %v doesn't exist.", prefixSet.PrefixSetName, - prefixSet.PrefixList[0].Address, prefixSet.PrefixList[0].Masklength, prefixSet.PrefixList[0].MasklengthRange) + prefix.Address, prefix.Masklength, prefix.MasklengthRange) } else { - copy(conPrefixSetList[idxPrefixSet].PrefixList[idxPrefix:], conPrefixSetList[idxPrefixSet].PrefixList[idxPrefix+1:]) - conPrefixSetList[idxPrefixSet].PrefixList = conPrefixSetList[idxPrefixSet].PrefixList[:len(conPrefixSetList[idxPrefixSet].PrefixList)-1] + conPrefixSetList[idxPrefixSet].PrefixList = + append(conPrefixSetList[idxPrefixSet].PrefixList[:idxPrefix], conPrefixSetList[idxPrefixSet].PrefixList[idxPrefix+1:]...) } } } else { @@ -418,8 +436,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { if idxPrefixSet == -1 { result.ResponseErr = fmt.Errorf("Policy prefix that has %v doesn't exist.", prefixSet.PrefixSetName) } else { - copy(conPrefixSetList[idxPrefixSet:], conPrefixSetList[idxPrefixSet+1:]) - conPrefixSetList = conPrefixSetList[:len(conPrefixSetList)-1] + conPrefixSetList = append(conPrefixSetList[:idxPrefixSet], conPrefixSetList[idxPrefixSet+1:]...) } } server.routingPolicy.DefinedSets.PrefixSetList = conPrefixSetList @@ -511,10 +528,9 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { result.ResponseErr = fmt.Errorf("Policy neighbor that has %v %v doesn't exist.", neighborSet.NeighborSetName, neighborSet.NeighborInfoList[0].Address) } else { - copy(conNeighborSetList[idxNeighborSet].NeighborInfoList[idxNeighbor:], - conNeighborSetList[idxNeighborSet].NeighborInfoList[idxNeighbor+1:]) conNeighborSetList[idxNeighborSet].NeighborInfoList = - conNeighborSetList[idxNeighborSet].NeighborInfoList[:len(conNeighborSetList[idxNeighborSet].NeighborInfoList)-1] + append(conNeighborSetList[idxNeighborSet].NeighborInfoList[:idxNeighbor], + conNeighborSetList[idxNeighborSet].NeighborInfoList[idxNeighbor+1:]...) } } } else { @@ -528,8 +544,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { if idxNeighborSet == -1 { result.ResponseErr = fmt.Errorf("Policy neighbor %v doesn't exist.", neighborSet.NeighborSetName) } else { - copy(conNeighborSetList[idxNeighborSet:], conNeighborSetList[idxNeighborSet+1:]) - conNeighborSetList = conNeighborSetList[:len(conNeighborSetList)-1] + conNeighborSetList = append(conNeighborSetList[:idxNeighborSet], conNeighborSetList[idxNeighborSet+1:]...) } } server.routingPolicy.DefinedSets.NeighborSetList = conNeighborSetList @@ -583,6 +598,8 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { close(grpcReq.ResponseCh) case REQ_POLICY_ROUTEPOLICY_ADD: reqPolicy := grpcReq.Data.(*api.PolicyDefinition) + reqConditions := reqPolicy.StatementList[0].Conditions + reqActions := reqPolicy.StatementList[0].Actions conPolicyList := server.routingPolicy.PolicyDefinitionList result := &GrpcResponse{} _, policyDef := policy.PolicyDefinitionToConfigStruct(reqPolicy) @@ -595,13 +612,23 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { conPolicyList[idxPolicy].StatementList = append(conPolicyList[idxPolicy].StatementList, statement) } else { - if reqPolicy.StatementList[0].Conditions != nil { - conPolicyList[idxPolicy].StatementList[idxStatement].Conditions = - statement.Conditions + conStatement := &conPolicyList[idxPolicy].StatementList[idxStatement] + if reqConditions != nil { + if reqConditions.MatchPrefixSet != nil { + conStatement.Conditions.MatchPrefixSet = statement.Conditions.MatchPrefixSet + } + if reqConditions.MatchNeighborSet != nil { + conStatement.Conditions.MatchNeighborSet = statement.Conditions.MatchNeighborSet + } + if reqConditions.MatchSetOptions != "" { + conStatement.Conditions.MatchSetOptions = statement.Conditions.MatchSetOptions + } + if reqConditions.MatchAsPathLength != nil { + conStatement.Conditions.BgpConditions.AsPathLength = statement.Conditions.BgpConditions.AsPathLength + } } - if reqPolicy.StatementList[0].Actions != nil { - conPolicyList[idxPolicy].StatementList[idxStatement].Actions = - statement.Actions + if reqActions != nil { + conStatement.Actions = statement.Actions } } } @@ -623,10 +650,8 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { if idxStatement == -1 { result.ResponseErr = fmt.Errorf("Policy Statment that has %v doesn't exist.", policyDef.StatementList[0].Name) } else { - copy(conPolicyList[idxPolicy].StatementList[idxStatement:], - conPolicyList[idxPolicy].StatementList[idxStatement+1:]) conPolicyList[idxPolicy].StatementList = - conPolicyList[idxPolicy].StatementList[:len(conPolicyList[idxPolicy].StatementList)-1] + append(conPolicyList[idxPolicy].StatementList[:idxStatement], conPolicyList[idxPolicy].StatementList[idxStatement+1:]...) } } } else { @@ -640,8 +665,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { if idxPolicy == -1 { result.ResponseErr = fmt.Errorf("Policy that has %v doesn't exist.", policyDef.Name) } else { - copy(conPolicyList[idxPolicy:], conPolicyList[idxPolicy+1:]) - conPolicyList = conPolicyList[:len(conPolicyList)-1] + conPolicyList = append(conPolicyList[:idxPolicy], conPolicyList[idxPolicy+1:]...) } } server.routingPolicy.PolicyDefinitionList = conPolicyList |