diff options
author | Hiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp> | 2015-06-29 18:01:58 +0900 |
---|---|---|
committer | Hiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp> | 2015-07-01 14:26:35 +0900 |
commit | ae8862599082f6bd70423df255db6f4769cad5a1 (patch) | |
tree | 36d326500782cc15b68682a37ca39d1486c4534c | |
parent | 046d96de655f2c75a43038ab75b78df93d031450 (diff) |
cli: support distribute policy
-rw-r--r-- | api/gobgp.pb.go | 17 | ||||
-rw-r--r-- | api/gobgp.proto | 2 | ||||
-rw-r--r-- | gobgp/common.go | 1 | ||||
-rw-r--r-- | gobgp/neighbor.go | 12 | ||||
-rw-r--r-- | server/grpc_server.go | 6 | ||||
-rw-r--r-- | server/server.go | 91 |
6 files changed, 89 insertions, 40 deletions
diff --git a/api/gobgp.pb.go b/api/gobgp.pb.go index cc2f5a4a..e0cde493 100644 --- a/api/gobgp.pb.go +++ b/api/gobgp.pb.go @@ -1196,10 +1196,12 @@ func (m *PolicyDefinition) GetStatementList() []*Statement { } type ApplyPolicy struct { - ImportPolicies []*PolicyDefinition `protobuf:"bytes,1,rep,name=import_policies" json:"import_policies,omitempty"` - DefaultImportPolicy string `protobuf:"bytes,2,opt,name=default_import_policy" json:"default_import_policy,omitempty"` - ExportPolicies []*PolicyDefinition `protobuf:"bytes,3,rep,name=export_policies" json:"export_policies,omitempty"` - DefaultExportPolicy string `protobuf:"bytes,4,opt,name=default_export_policy" json:"default_export_policy,omitempty"` + ImportPolicies []*PolicyDefinition `protobuf:"bytes,1,rep,name=import_policies" json:"import_policies,omitempty"` + DefaultImportPolicy string `protobuf:"bytes,2,opt,name=default_import_policy" json:"default_import_policy,omitempty"` + ExportPolicies []*PolicyDefinition `protobuf:"bytes,3,rep,name=export_policies" json:"export_policies,omitempty"` + DefaultExportPolicy string `protobuf:"bytes,4,opt,name=default_export_policy" json:"default_export_policy,omitempty"` + DistributePolicies []*PolicyDefinition `protobuf:"bytes,5,rep,name=distribute_policies" json:"distribute_policies,omitempty"` + DefaultDistributePolicy string `protobuf:"bytes,6,opt,name=default_distribute_policy" json:"default_distribute_policy,omitempty"` } func (m *ApplyPolicy) Reset() { *m = ApplyPolicy{} } @@ -1220,6 +1222,13 @@ func (m *ApplyPolicy) GetExportPolicies() []*PolicyDefinition { return nil } +func (m *ApplyPolicy) GetDistributePolicies() []*PolicyDefinition { + if m != nil { + return m.DistributePolicies + } + return nil +} + func init() { proto.RegisterEnum("api.Resource", Resource_name, Resource_value) proto.RegisterEnum("api.Operation", Operation_name, Operation_value) diff --git a/api/gobgp.proto b/api/gobgp.proto index dce05d8b..1467ab51 100644 --- a/api/gobgp.proto +++ b/api/gobgp.proto @@ -442,4 +442,6 @@ message ApplyPolicy { string default_import_policy = 2; repeated PolicyDefinition export_policies = 3; string default_export_policy = 4; + repeated PolicyDefinition distribute_policies = 5; + string default_distribute_policy = 6; } diff --git a/gobgp/common.go b/gobgp/common.go index 0ae61af7..8eccdfaf 100644 --- a/gobgp/common.go +++ b/gobgp/common.go @@ -52,6 +52,7 @@ const ( CMD_ACTIONS = "actions" CMD_IMPORT = "import" CMD_EXPORT = "export" + CMD_DISTRIBUTE = "distribute" CMD_MONITOR = "monitor" ) diff --git a/gobgp/neighbor.go b/gobgp/neighbor.go index 9935d1a0..3bf68352 100644 --- a/gobgp/neighbor.go +++ b/gobgp/neighbor.go @@ -598,8 +598,8 @@ func showNeighborPolicy(remoteIP net.IP) error { fmt.Printf("DefaultImportPolicy: %s\n", ap.DefaultImportPolicy) fmt.Printf("DefaultExportPolicy: %s\n", ap.DefaultExportPolicy) + fmt.Printf("DefaultDistributePolicy: %s\n", ap.DefaultDistributePolicy) fmt.Printf("ImportPolicies:\n") - for _, inPolicy := range ap.ImportPolicies { fmt.Printf(" PolicyName %s:\n", inPolicy.PolicyDefinitionName) showPolicyStatement(" ", inPolicy) @@ -609,6 +609,11 @@ func showNeighborPolicy(remoteIP net.IP) error { fmt.Printf(" PolicyName %s:\n", outPolicy.PolicyDefinitionName) showPolicyStatement(" ", outPolicy) } + fmt.Printf("DistributePolicies:\n") + for _, distPolicy := range ap.DistributePolicies { + fmt.Printf(" PolicyName %s:\n", distPolicy.PolicyDefinitionName) + showPolicyStatement(" ", distPolicy) + } return nil } @@ -632,7 +637,7 @@ func modNeighborPolicy(remoteIP net.IP, cmdType string, eArg []string) error { switch cmdType { case CMD_ADD: if len(eArg) < 4 { - return fmt.Errorf("Usage: gobgp neighbor <ipaddr> policy %s {%s|%s} <policies> {%s|%s}", cmdType, CMD_IMPORT, CMD_EXPORT, policy.ROUTE_ACCEPT, policy.ROUTE_REJECT) + return fmt.Errorf("Usage: gobgp neighbor <ipaddr> policy %s {%s|%s|%s} <policies> {%s|%s}", cmdType, CMD_IMPORT, CMD_EXPORT, CMD_DISTRIBUTE, policy.ROUTE_ACCEPT, policy.ROUTE_REJECT) } policies := parsePolicy(eArg[1]) defaultPolicy, err := parseRouteAction(eArg[2]) @@ -646,6 +651,9 @@ func modNeighborPolicy(remoteIP net.IP, cmdType string, eArg []string) error { case CMD_EXPORT: pol.ExportPolicies = policies pol.DefaultExportPolicy = defaultPolicy + case CMD_DISTRIBUTE: + pol.DistributePolicies = policies + pol.DefaultDistributePolicy = defaultPolicy } operation = api.Operation_ADD diff --git a/server/grpc_server.go b/server/grpc_server.go index 12a3a87a..25af6dc7 100644 --- a/server/grpc_server.go +++ b/server/grpc_server.go @@ -43,8 +43,10 @@ const ( REQ_NEIGHBOR_POLICY REQ_NEIGHBOR_POLICY_ADD_IMPORT REQ_NEIGHBOR_POLICY_ADD_EXPORT + REQ_NEIGHBOR_POLICY_ADD_DISTRIBUTE REQ_NEIGHBOR_POLICY_DEL_IMPORT REQ_NEIGHBOR_POLICY_DEL_EXPORT + REQ_NEIGHBOR_POLICY_DEL_DISTRIBUTE REQ_GLOBAL_RIB REQ_GLOBAL_ADD REQ_GLOBAL_DELETE @@ -382,6 +384,8 @@ func (s *Server) ModNeighborPolicy(stream api.Grpc_ModNeighborPolicyServer) erro reqType = REQ_NEIGHBOR_POLICY_ADD_IMPORT case "export": reqType = REQ_NEIGHBOR_POLICY_ADD_EXPORT + case "distribute": + reqType = REQ_NEIGHBOR_POLICY_ADD_DISTRIBUTE } case api.Operation_DEL: switch arg.Name { @@ -389,6 +393,8 @@ func (s *Server) ModNeighborPolicy(stream api.Grpc_ModNeighborPolicyServer) erro reqType = REQ_NEIGHBOR_POLICY_DEL_IMPORT case "export": reqType = REQ_NEIGHBOR_POLICY_DEL_EXPORT + case "distribute": + reqType = REQ_NEIGHBOR_POLICY_DEL_DISTRIBUTE } } req := NewGrpcRequest(reqType, arg.RouterId, rf, arg.ApplyPolicy) diff --git a/server/server.go b/server/server.go index 8fd32193..9c60f0ce 100644 --- a/server/server.go +++ b/server/server.go @@ -1031,61 +1031,68 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { } resInPolicies := []*api.PolicyDefinition{} resOutPolicies := []*api.PolicyDefinition{} + resDistPolicies := []*api.PolicyDefinition{} pdList := server.routingPolicy.PolicyDefinitionList df := server.routingPolicy.DefinedSets - // 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.config.ApplyPolicy.ImportPolicies - for _, conInPolicyName := range conInPolicyNames { - match := false - for _, pd := range pdList { - if conInPolicyName == pd.Name { - match = true - resInPolicies = append(resInPolicies, policy.PolicyDefinitionToApiStruct(pd, df)) - break + + extract := func(policies []string) []*api.PolicyDefinition { + extracted := []*api.PolicyDefinition{} + for _, conInPolicyName := range policies { + match := false + for _, pd := range pdList { + if conInPolicyName == pd.Name { + match = true + extracted = append(extracted, policy.PolicyDefinitionToApiStruct(pd, df)) + break + } + } + if !match { + extracted = append(extracted, &api.PolicyDefinition{PolicyDefinitionName: conInPolicyName}) } } - if !match { - resInPolicies = append(resInPolicies, &api.PolicyDefinition{PolicyDefinitionName: conInPolicyName}) - } + return extracted } + + // 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.config.ApplyPolicy.ImportPolicies + resInPolicies = extract(conInPolicyNames) + // 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. conOutPolicyNames := peer.config.ApplyPolicy.ExportPolicies - for _, conOutPolicyName := range conOutPolicyNames { - match := false - for _, pd := range pdList { - if conOutPolicyName == pd.Name { - match = true - resOutPolicies = append(resOutPolicies, policy.PolicyDefinitionToApiStruct(pd, df)) - break - } - } - if !match { - resOutPolicies = append(resOutPolicies, &api.PolicyDefinition{PolicyDefinitionName: conOutPolicyName}) - } - } + resOutPolicies = extract(conOutPolicyNames) + + distPolicyNames := peer.config.ApplyPolicy.DistributePolicies + resDistPolicies = extract(distPolicyNames) defaultInPolicy := policy.ROUTE_REJECT defaultOutPolicy := policy.ROUTE_REJECT + defaultDistPolicy := policy.ROUTE_REJECT if loc.defaultImportPolicy == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE { defaultInPolicy = policy.ROUTE_ACCEPT } if loc.defaultExportPolicy == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE { defaultOutPolicy = policy.ROUTE_ACCEPT } + if peer.defaultDistributePolicy == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE { + defaultDistPolicy = policy.ROUTE_ACCEPT + } result := &GrpcResponse{ Data: &api.ApplyPolicy{ - DefaultImportPolicy: defaultInPolicy, - ImportPolicies: resInPolicies, - DefaultExportPolicy: defaultOutPolicy, - ExportPolicies: resOutPolicies, + DefaultImportPolicy: defaultInPolicy, + ImportPolicies: resInPolicies, + DefaultExportPolicy: defaultOutPolicy, + ExportPolicies: resOutPolicies, + DefaultDistributePolicy: defaultDistPolicy, + DistributePolicies: resDistPolicies, }, } 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: + case REQ_NEIGHBOR_POLICY_ADD_IMPORT, REQ_NEIGHBOR_POLICY_ADD_EXPORT, REQ_NEIGHBOR_POLICY_ADD_DISTRIBUTE, + REQ_NEIGHBOR_POLICY_DEL_IMPORT, REQ_NEIGHBOR_POLICY_DEL_EXPORT, REQ_NEIGHBOR_POLICY_DEL_DISTRIBUTE: peer, err := server.checkNeighborRequest(grpcReq) if err != nil { break @@ -1093,7 +1100,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { reqApplyPolicy := grpcReq.Data.(*api.ApplyPolicy) reqPolicyMap := server.policyMap applyPolicy := &peer.config.ApplyPolicy - var defInPolicy, defOutPolicy config.DefaultPolicyType + var defInPolicy, defOutPolicy, defDistPolicy config.DefaultPolicyType if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_ADD_IMPORT { if reqApplyPolicy.DefaultImportPolicy != policy.ROUTE_ACCEPT { defInPolicy = config.DEFAULT_POLICY_TYPE_REJECT_ROUTE @@ -1106,15 +1113,31 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { } peer.config.ApplyPolicy.DefaultExportPolicy = defOutPolicy applyPolicy.ExportPolicies = policy.PoliciesToString(reqApplyPolicy.ExportPolicies) + } else if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_ADD_DISTRIBUTE { + if reqApplyPolicy.DefaultDistributePolicy != policy.ROUTE_ACCEPT { + defDistPolicy = config.DEFAULT_POLICY_TYPE_REJECT_ROUTE + } + peer.config.ApplyPolicy.DefaultDistributePolicy = defDistPolicy + applyPolicy.DistributePolicies = policy.PoliciesToString(reqApplyPolicy.DistributePolicies) } else if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_DEL_IMPORT { peer.config.ApplyPolicy.DefaultImportPolicy = config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE peer.config.ApplyPolicy.ImportPolicies = make([]string, 0) } else if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_DEL_EXPORT { peer.config.ApplyPolicy.DefaultExportPolicy = config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE peer.config.ApplyPolicy.ExportPolicies = make([]string, 0) + } else if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_DEL_DISTRIBUTE { + peer.config.ApplyPolicy.DefaultDistributePolicy = config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE + peer.config.ApplyPolicy.DistributePolicies = make([]string, 0) } - loc := server.localRibMap[peer.config.NeighborAddress.String()] - loc.setPolicy(peer, reqPolicyMap) + + if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_ADD_DISTRIBUTE || + grpcReq.RequestType == REQ_NEIGHBOR_POLICY_DEL_DISTRIBUTE { + peer.setPolicy(reqPolicyMap) + } else { + loc := server.localRibMap[peer.config.NeighborAddress.String()] + loc.setPolicy(peer, reqPolicyMap) + } + grpcReq.ResponseCh <- &GrpcResponse{} close(grpcReq.ResponseCh) |