diff options
author | Naoto Hanaue <hanaue.naoto@po.ntts.co.jp> | 2015-05-25 11:50:42 +0900 |
---|---|---|
committer | Naoto Hanaue <hanaue.naoto@po.ntts.co.jp> | 2015-05-26 17:28:44 +0900 |
commit | fd4f6a91efb81646281ae8175433872627ec8611 (patch) | |
tree | f657b1c3bec445ed48dd82a009f61a34533209db /server | |
parent | fa5d2fe25ee3670a5ba2e66431dfd617f48737af (diff) |
cli: add the add/del commands to routepolicy
% gobgp -u 10.0.255.1 policy routepolicy
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
% gobgp -u 10.0.255.1 policy routepolicy add policy1 st0 conditions --prefix ps2 --neighbor ns0 --aspath-len eq,5 --option all
% gobgp -u 10.0.255.1 policy routepolicy add policy1 st0 actions --route-action accept
% gobgp -u 10.0.255.1 policy routepolicy del policy0
% gobgp -u 10.0.255.1 policy routepolicy
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
Diffstat (limited to 'server')
-rw-r--r-- | server/grpc_server.go | 40 | ||||
-rw-r--r-- | server/peer.go | 12 | ||||
-rw-r--r-- | server/server.go | 74 |
3 files changed, 124 insertions, 2 deletions
diff --git a/server/grpc_server.go b/server/grpc_server.go index dbd1be1e..f1d040a2 100644 --- a/server/grpc_server.go +++ b/server/grpc_server.go @@ -56,6 +56,9 @@ const ( REQ_POLICY_NEIGHBORS_DELETE REQ_POLICY_ROUTEPOLICIES REQ_POLICY_ROUTEPOLICY + REQ_POLICY_ROUTEPOLICY_ADD + REQ_POLICY_ROUTEPOLICY_DELETE + REQ_POLICY_ROUTEPOLICIES_DELETE ) const GRPC_PORT = 8080 @@ -390,6 +393,28 @@ func (s *Server) modPolicy(arg *api.PolicyArguments, stream interface{}) error { err = stream.(api.Grpc_ModPolicyNeighborServer).Send(&api.Error{ Code: api.Error_SUCCESS, }) + case api.Resource_POLICY_ROUTEPOLICY: + switch arg.Operation { + case api.Operation_ADD: + reqType = REQ_POLICY_ROUTEPOLICY_ADD + case api.Operation_DEL: + reqType = REQ_POLICY_ROUTEPOLICY_DELETE + case api.Operation_DEL_ALL: + reqType = REQ_POLICY_ROUTEPOLICIES_DELETE + default: + return fmt.Errorf("unsupported operation: %s", arg.Operation) + } + req := NewGrpcRequest(reqType, "", rf, arg.PolicyDifinition) + s.bgpServerCh <- req + + res := <-req.ResponseCh + if err := res.Err(); err != nil { + log.Debug(err.Error()) + return err + } + err = stream.(api.Grpc_ModPolicyRoutePolicyServer).Send(&api.Error{ + Code: api.Error_SUCCESS, + }) default: return fmt.Errorf("unsupported resource type: %v", arg.Resource) } @@ -476,6 +501,21 @@ func (s *Server) GetPolicyRoutePolicy(ctx context.Context, arg *api.PolicyArgume return data.(*api.PolicyDefinition), nil } +func (s *Server) ModPolicyRoutePolicy(stream api.Grpc_ModPolicyRoutePolicyServer) error { + for { + arg, err := stream.Recv() + if err == io.EOF { + return nil + } else if err != nil { + return err + } + if err := s.modPolicy(arg, stream); err != nil { + return err + } + return nil + } +} + type GrpcRequest struct { RequestType int RemoteAddr string diff --git a/server/peer.go b/server/peer.go index f83bbc3e..02585e11 100644 --- a/server/peer.go +++ b/server/peer.go @@ -624,10 +624,18 @@ func (peer *Peer) handleGrpc(grpcReq *GrpcRequest) { resOutPolicies = append(resOutPolicies, &api.PolicyDefinition{PolicyDefinitionName: conOutPolicyName}) } } + defaultInPolicy := "REJECT" + defaultOutPolicy := "REJECT" + if peer.defaultImportPolicy == 0 { + defaultInPolicy = "ACCEPT" + } + if peer.defaultExportPolicy == 0 { + defaultOutPolicy = "ACCEPT" + } result.Data = &api.ApplyPolicy{ - DefaultImportPolicy: int64(peer.defaultImportPolicy), + DefaultImportPolicy: defaultInPolicy, ImportPolicies: resInPolicies, - DefaultExportPolicy: int64(peer.defaultExportPolicy), + DefaultExportPolicy: defaultOutPolicy, ExportPolicies: resOutPolicies, } grpcReq.ResponseCh <- result diff --git a/server/server.go b/server/server.go index adc2bc57..88ff5a99 100644 --- a/server/server.go +++ b/server/server.go @@ -581,5 +581,79 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) + case REQ_POLICY_ROUTEPOLICY_ADD: + reqPolicy := grpcReq.Data.(*api.PolicyDefinition) + conPolicyList := server.routingPolicy.PolicyDefinitionList + result := &GrpcResponse{} + _, policyDef := policy.PolicyDefinitionToConfigStruct(reqPolicy) + idxPolicy, idxStatement := policy.IndexOfPolicyDefinition(conPolicyList, policyDef) + if idxPolicy == -1 { + conPolicyList = append(conPolicyList, policyDef) + } else { + statement := policyDef.StatementList[0] + if idxStatement == -1 { + conPolicyList[idxPolicy].StatementList = + append(conPolicyList[idxPolicy].StatementList, statement) + } else { + if reqPolicy.StatementList[0].Conditions != nil { + conPolicyList[idxPolicy].StatementList[idxStatement].Conditions = + statement.Conditions + } + if reqPolicy.StatementList[0].Actions != nil { + conPolicyList[idxPolicy].StatementList[idxStatement].Actions = + statement.Actions + } + } + } + server.routingPolicy.PolicyDefinitionList = conPolicyList + server.handlePolicy(server.routingPolicy) + grpcReq.ResponseCh <- result + close(grpcReq.ResponseCh) + + case REQ_POLICY_ROUTEPOLICY_DELETE: + reqPolicy := grpcReq.Data.(*api.PolicyDefinition) + conPolicyList := server.routingPolicy.PolicyDefinitionList + result := &GrpcResponse{} + isStatement, policyDef := policy.PolicyDefinitionToConfigStruct(reqPolicy) + idxPolicy, idxStatement := policy.IndexOfPolicyDefinition(conPolicyList, policyDef) + if isStatement { + if idxPolicy == -1 { + result.ResponseErr = fmt.Errorf("Policy that has %v doesn't exist.", policyDef.Name) + } else { + 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] + } + } + } else { + idxPolicy := -1 + for i, conPolicy := range conPolicyList { + if conPolicy.Name == reqPolicy.PolicyDefinitionName { + idxPolicy = i + break + } + } + 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] + } + } + server.routingPolicy.PolicyDefinitionList = conPolicyList + server.handlePolicy(server.routingPolicy) + grpcReq.ResponseCh <- result + close(grpcReq.ResponseCh) + + case REQ_POLICY_ROUTEPOLICIES_DELETE: + result := &GrpcResponse{} + server.routingPolicy.PolicyDefinitionList = make([]config.PolicyDefinition, 0) + server.handlePolicy(server.routingPolicy) + grpcReq.ResponseCh <- result + close(grpcReq.ResponseCh) } } |