From 08d8503c6238ce69112ec331fe7f038690df8ca6 Mon Sep 17 00:00:00 2001 From: ISHIDA Wataru Date: Mon, 12 Oct 2015 06:22:27 +0900 Subject: api: support per in/import/export policy configuration Signed-off-by: ISHIDA Wataru --- server/grpc_server.go | 52 ++++------------ server/server.go | 164 ++++++++++++++++++++++++-------------------------- 2 files changed, 89 insertions(+), 127 deletions(-) (limited to 'server') diff --git a/server/grpc_server.go b/server/grpc_server.go index b3da4b3d..59d1819c 100644 --- a/server/grpc_server.go +++ b/server/grpc_server.go @@ -41,12 +41,7 @@ const ( REQ_NEIGHBOR_ENABLE REQ_NEIGHBOR_DISABLE REQ_NEIGHBOR_POLICY - REQ_NEIGHBOR_POLICY_ADD_IMPORT - REQ_NEIGHBOR_POLICY_ADD_EXPORT - REQ_NEIGHBOR_POLICY_ADD_IN - REQ_NEIGHBOR_POLICY_DEL_IMPORT - REQ_NEIGHBOR_POLICY_DEL_EXPORT - REQ_NEIGHBOR_POLICY_DEL_IN + REQ_MOD_NEIGHBOR_POLICY REQ_GLOBAL_RIB REQ_POLICY_PREFIX REQ_POLICY_PREFIXES @@ -269,16 +264,18 @@ func (s *Server) ModPath(stream api.GobgpApi_ModPathServer) error { return err } -func (s *Server) GetNeighborPolicy(ctx context.Context, arg *api.Arguments) (*api.ApplyPolicy, error) { - if arg.Resource != api.Resource_LOCAL && arg.Resource != api.Resource_GLOBAL { +func (s *Server) GetNeighborPolicy(ctx context.Context, arg *api.PolicyArguments) (*api.ApplyPolicy, error) { + r := 0 + switch arg.Resource { + case api.Resource_GLOBAL: + r = REQ_GLOBAL_POLICY + case api.Resource_POLICY_NEIGHBOR: + r = REQ_NEIGHBOR_POLICY + default: return nil, fmt.Errorf("unsupported resource: %s", arg.Resource) } - var req *GrpcRequest - if arg.Resource == api.Resource_LOCAL { - req = NewGrpcRequest(REQ_NEIGHBOR_POLICY, arg.Name, bgp.RouteFamily(arg.Rf), nil) - } else { - req = NewGrpcRequest(REQ_GLOBAL_POLICY, "", bgp.RouteFamily(arg.Rf), nil) - } + + req := NewGrpcRequest(r, arg.NeighborAddress, bgp.RouteFamily(0), arg) s.bgpServerCh <- req res := <-req.ResponseCh @@ -298,32 +295,7 @@ func (s *Server) ModNeighborPolicy(stream api.GobgpApi_ModNeighborPolicyServer) 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 "in": - reqType = REQ_NEIGHBOR_POLICY_ADD_IN - } - case api.Operation_DEL: - switch arg.Name { - case "import": - reqType = REQ_NEIGHBOR_POLICY_DEL_IMPORT - case "export": - reqType = REQ_NEIGHBOR_POLICY_DEL_EXPORT - case "in": - reqType = REQ_NEIGHBOR_POLICY_DEL_IN - } - } - req := NewGrpcRequest(reqType, arg.NeighborAddress, rf, arg.ApplyPolicy) + req := NewGrpcRequest(REQ_MOD_NEIGHBOR_POLICY, arg.NeighborAddress, bgp.RouteFamily(0), arg) s.bgpServerCh <- req res := <-req.ResponseCh if err := res.Err(); err != nil { diff --git a/server/server.go b/server/server.go index 35f26a86..77d8fb22 100644 --- a/server/server.go +++ b/server/server.go @@ -1547,114 +1547,104 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { close(grpcReq.ResponseCh) case REQ_NEIGHBOR_POLICY, REQ_GLOBAL_POLICY: - var in, imp, exp []*api.PolicyDefinition - var inD, impD, expD api.RouteAction - - extract := func(policyNames []string) []*api.PolicyDefinition { - pdList := server.routingPolicy.PolicyDefinitions.PolicyDefinitionList - df := server.routingPolicy.DefinedSets - extracted := []*api.PolicyDefinition{} - for _, policyName := range policyNames { - match := false - for _, pd := range pdList { - if policyName == pd.Name { - match = true - extracted = append(extracted, table.PolicyDefinitionToApiStruct(pd, df)) - break - } - } - if !match { - extracted = append(extracted, &api.PolicyDefinition{PolicyDefinitionName: policyName}) - } - } - return extracted - } - - if grpcReq.RequestType == REQ_NEIGHBOR_POLICY { + arg := grpcReq.Data.(*api.PolicyArguments) + var names []string + def := api.RouteAction_REJECT + var applyPolicy config.ApplyPolicy + switch grpcReq.RequestType { + case REQ_NEIGHBOR_POLICY: peer, err := server.checkNeighborRequest(grpcReq) if err != nil { break } - // 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. - conImportPolicyNames := peer.conf.ApplyPolicy.ApplyPolicyConfig.ImportPolicy - imp = extract(conImportPolicyNames) - - // 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. - conExportPolicyNames := peer.conf.ApplyPolicy.ApplyPolicyConfig.ExportPolicy - exp = extract(conExportPolicyNames) - - inPolicyNames := peer.conf.ApplyPolicy.ApplyPolicyConfig.InPolicy - in = extract(inPolicyNames) - - impD = peer.GetDefaultPolicy(table.POLICY_DIRECTION_IMPORT).ToApiStruct() - expD = peer.GetDefaultPolicy(table.POLICY_DIRECTION_EXPORT).ToApiStruct() - inD = peer.GetDefaultPolicy(table.POLICY_DIRECTION_IN).ToApiStruct() - } else { - names := server.bgpConfig.Global.ApplyPolicy.ApplyPolicyConfig.ImportPolicy - imp = extract(names) - - names = server.bgpConfig.Global.ApplyPolicy.ApplyPolicyConfig.ExportPolicy - exp = extract(names) + applyPolicy = peer.conf.ApplyPolicy + case REQ_GLOBAL_RIB: + applyPolicy = server.bgpConfig.Global.ApplyPolicy + default: + } + switch arg.ApplyPolicy.Type { + case api.PolicyType_IMPORT: + names = applyPolicy.ApplyPolicyConfig.ImportPolicy + if applyPolicy.ApplyPolicyConfig.DefaultImportPolicy == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE { + def = api.RouteAction_ACCEPT + } + case api.PolicyType_EXPORT: + names = applyPolicy.ApplyPolicyConfig.ExportPolicy + if applyPolicy.ApplyPolicyConfig.DefaultExportPolicy == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE { + def = api.RouteAction_ACCEPT + } + case api.PolicyType_IN: + names = applyPolicy.ApplyPolicyConfig.InPolicy + if applyPolicy.ApplyPolicyConfig.DefaultInPolicy == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE { + def = api.RouteAction_ACCEPT + } + } + policies := make([]*api.PolicyDefinition, 0, len(names)) - impD = server.globalRib.GetDefaultPolicy(table.POLICY_DIRECTION_IMPORT).ToApiStruct() - expD = server.globalRib.GetDefaultPolicy(table.POLICY_DIRECTION_EXPORT).ToApiStruct() + pdList := server.routingPolicy.PolicyDefinitions.PolicyDefinitionList + df := server.routingPolicy.DefinedSets + for _, name := range names { + match := false + for _, pd := range pdList { + if name == pd.Name { + match = true + policies = append(policies, table.PolicyDefinitionToApiStruct(pd, df)) + break + } + } + if !match { + policies = append(policies, &api.PolicyDefinition{PolicyDefinitionName: name}) + } } result := &GrpcResponse{ Data: &api.ApplyPolicy{ - DefaultImportPolicy: impD, - ImportPolicies: imp, - DefaultExportPolicy: expD, - ExportPolicies: exp, - DefaultInPolicy: inD, - InPolicies: in, + Policies: policies, + Default: def, }, } grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) - case REQ_NEIGHBOR_POLICY_ADD_IMPORT, REQ_NEIGHBOR_POLICY_ADD_EXPORT, REQ_NEIGHBOR_POLICY_ADD_IN, - REQ_NEIGHBOR_POLICY_DEL_IMPORT, REQ_NEIGHBOR_POLICY_DEL_EXPORT, REQ_NEIGHBOR_POLICY_DEL_IN: + case REQ_MOD_NEIGHBOR_POLICY: peer, err := server.checkNeighborRequest(grpcReq) if err != nil { break } result := &GrpcResponse{} - reqApplyPolicy := grpcReq.Data.(*api.ApplyPolicy) + arg := grpcReq.Data.(*api.PolicyArguments) reqPolicyMap := server.policyMap - applyPolicy := &peer.conf.ApplyPolicy.ApplyPolicyConfig - var defInPolicy, defOutPolicy, defDistPolicy config.DefaultPolicyType - if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_ADD_IMPORT { - if reqApplyPolicy.DefaultImportPolicy != api.RouteAction_ACCEPT { - defInPolicy = config.DEFAULT_POLICY_TYPE_REJECT_ROUTE - } - applyPolicy.DefaultImportPolicy = defInPolicy - applyPolicy.ImportPolicy = table.PoliciesToString(reqApplyPolicy.ImportPolicies) - } else if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_ADD_EXPORT { - if reqApplyPolicy.DefaultExportPolicy != api.RouteAction_ACCEPT { - defOutPolicy = config.DEFAULT_POLICY_TYPE_REJECT_ROUTE - } - applyPolicy.DefaultExportPolicy = defOutPolicy - applyPolicy.ExportPolicy = table.PoliciesToString(reqApplyPolicy.ExportPolicies) - } else if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_ADD_IN { - if reqApplyPolicy.DefaultInPolicy != api.RouteAction_ACCEPT { - defDistPolicy = config.DEFAULT_POLICY_TYPE_REJECT_ROUTE - } - applyPolicy.DefaultInPolicy = defDistPolicy - applyPolicy.InPolicy = table.PoliciesToString(reqApplyPolicy.InPolicies) - } else if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_DEL_IMPORT { - applyPolicy.DefaultImportPolicy = config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE - applyPolicy.ImportPolicy = make([]string, 0) - } else if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_DEL_EXPORT { - applyPolicy.DefaultExportPolicy = config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE - applyPolicy.ExportPolicy = make([]string, 0) - } else if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_DEL_IN { - applyPolicy.DefaultInPolicy = config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE - applyPolicy.InPolicy = make([]string, 0) + applyPolicy := peer.conf.ApplyPolicy.ApplyPolicyConfig + def := config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE + switch arg.Operation { + case api.Operation_ADD: + if arg.ApplyPolicy.Default != api.RouteAction_REJECT { + def = config.DEFAULT_POLICY_TYPE_REJECT_ROUTE + } + switch arg.ApplyPolicy.Type { + case api.PolicyType_IMPORT: + applyPolicy.DefaultImportPolicy = def + applyPolicy.ImportPolicy = table.PoliciesToString(arg.ApplyPolicy.Policies) + case api.PolicyType_EXPORT: + applyPolicy.DefaultExportPolicy = def + applyPolicy.ExportPolicy = table.PoliciesToString(arg.ApplyPolicy.Policies) + case api.PolicyType_IN: + applyPolicy.DefaultInPolicy = def + applyPolicy.InPolicy = table.PoliciesToString(arg.ApplyPolicy.Policies) + } + case api.Operation_DEL: + switch arg.ApplyPolicy.Type { + case api.PolicyType_IMPORT: + applyPolicy.DefaultImportPolicy = def + applyPolicy.ImportPolicy = []string{} + case api.PolicyType_EXPORT: + applyPolicy.DefaultExportPolicy = def + applyPolicy.ExportPolicy = []string{} + case api.PolicyType_IN: + applyPolicy.DefaultInPolicy = def + applyPolicy.InPolicy = []string{} + } } - peer.setPolicy(reqPolicyMap) grpcReq.ResponseCh <- result -- cgit v1.2.3