diff options
-rw-r--r-- | api/gobgp.pb.go | 53 | ||||
-rw-r--r-- | api/gobgp.proto | 10 | ||||
-rw-r--r-- | gobgp/main.go | 111 | ||||
-rw-r--r-- | policy/policy.go | 112 | ||||
-rw-r--r-- | server/grpc_server.go | 19 | ||||
-rw-r--r-- | server/peer.go | 41 | ||||
-rw-r--r-- | server/server.go | 39 |
7 files changed, 340 insertions, 45 deletions
diff --git a/api/gobgp.pb.go b/api/gobgp.pb.go index 8a54a2c7..2d8767a8 100644 --- a/api/gobgp.pb.go +++ b/api/gobgp.pb.go @@ -40,6 +40,7 @@ It has these top-level messages: Statement PolicyDefinition RoutingPolicy + ApplyPolicy */ package api @@ -1062,6 +1063,31 @@ func (m *RoutingPolicy) GetPolicyDifinition() []*PolicyDefinition { return nil } +type ApplyPolicy struct { + ImportPolicies []*PolicyDefinition `protobuf:"bytes,1,rep,name=import_policies" json:"import_policies,omitempty"` + DefaultImportPolicy int64 `protobuf:"varint,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 int64 `protobuf:"varint,4,opt,name=default_export_policy" json:"default_export_policy,omitempty"` +} + +func (m *ApplyPolicy) Reset() { *m = ApplyPolicy{} } +func (m *ApplyPolicy) String() string { return proto.CompactTextString(m) } +func (*ApplyPolicy) ProtoMessage() {} + +func (m *ApplyPolicy) GetImportPolicies() []*PolicyDefinition { + if m != nil { + return m.ImportPolicies + } + return nil +} + +func (m *ApplyPolicy) GetExportPolicies() []*PolicyDefinition { + if m != nil { + return m.ExportPolicies + } + return nil +} + func init() { proto.RegisterEnum("api.Resource", Resource_name, Resource_value) proto.RegisterEnum("api.Operation", Operation_name, Operation_value) @@ -1093,6 +1119,7 @@ type GrpcClient interface { Enable(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (*Error, error) Disable(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (*Error, error) ModPath(ctx context.Context, opts ...grpc.CallOption) (Grpc_ModPathClient, error) + GetNeighborPolicy(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (*ApplyPolicy, error) GetPolicyPrefixes(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (Grpc_GetPolicyPrefixesClient, error) GetPolicyPrefix(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (*PrefixSet, error) ModPolicyPrefix(ctx context.Context, opts ...grpc.CallOption) (Grpc_ModPolicyPrefixClient, error) @@ -1309,6 +1336,15 @@ func (x *grpcModPathClient) Recv() (*Error, error) { return m, nil } +func (c *grpcClient) GetNeighborPolicy(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (*ApplyPolicy, error) { + out := new(ApplyPolicy) + err := grpc.Invoke(ctx, "/api.Grpc/GetNeighborPolicy", in, out, c.cc, opts...) + if err != nil { + return nil, err + } + return out, nil +} + func (c *grpcClient) GetPolicyPrefixes(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (Grpc_GetPolicyPrefixesClient, error) { stream, err := grpc.NewClientStream(ctx, &_Grpc_serviceDesc.Streams[4], c.cc, "/api.Grpc/GetPolicyPrefixes", opts...) if err != nil { @@ -1478,6 +1514,7 @@ type GrpcServer interface { Enable(context.Context, *Arguments) (*Error, error) Disable(context.Context, *Arguments) (*Error, error) ModPath(Grpc_ModPathServer) error + GetNeighborPolicy(context.Context, *Arguments) (*ApplyPolicy, error) GetPolicyPrefixes(*PolicyArguments, Grpc_GetPolicyPrefixesServer) error GetPolicyPrefix(context.Context, *PolicyArguments) (*PrefixSet, error) ModPolicyPrefix(Grpc_ModPolicyPrefixServer) error @@ -1676,6 +1713,18 @@ func (x *grpcModPathServer) Recv() (*ModPathArguments, error) { return m, nil } +func _Grpc_GetNeighborPolicy_Handler(srv interface{}, ctx context.Context, codec grpc.Codec, buf []byte) (interface{}, error) { + in := new(Arguments) + if err := codec.Unmarshal(buf, in); err != nil { + return nil, err + } + out, err := srv.(GrpcServer).GetNeighborPolicy(ctx, in) + if err != nil { + return nil, err + } + return out, nil +} + func _Grpc_GetPolicyPrefixes_Handler(srv interface{}, stream grpc.ServerStream) error { m := new(PolicyArguments) if err := stream.RecvMsg(m); err != nil { @@ -1838,6 +1887,10 @@ var _Grpc_serviceDesc = grpc.ServiceDesc{ Handler: _Grpc_Disable_Handler, }, { + MethodName: "GetNeighborPolicy", + Handler: _Grpc_GetNeighborPolicy_Handler, + }, + { MethodName: "GetPolicyPrefix", Handler: _Grpc_GetPolicyPrefix_Handler, }, diff --git a/api/gobgp.proto b/api/gobgp.proto index 98a72228..362c619a 100644 --- a/api/gobgp.proto +++ b/api/gobgp.proto @@ -32,6 +32,7 @@ service Grpc { rpc Enable(Arguments) returns (Error) {} rpc Disable(Arguments) returns (Error) {} rpc ModPath(stream ModPathArguments) returns (stream Error) {} + rpc GetNeighborPolicy(Arguments) returns (ApplyPolicy) {} rpc GetPolicyPrefixes(PolicyArguments) returns (stream PrefixSet) {} rpc GetPolicyPrefix(PolicyArguments) returns (PrefixSet) {} rpc ModPolicyPrefix(stream PolicyArguments) returns (stream Error) {} @@ -382,7 +383,7 @@ message Statement { Actions actions = 3; } -message PolicyDefinition{ +message PolicyDefinition { string policy_definition_name = 1; repeated Statement statement_list = 2; } @@ -390,3 +391,10 @@ message PolicyDefinition{ message RoutingPolicy { repeated PolicyDefinition policy_difinition = 1; } + +message ApplyPolicy { + repeated PolicyDefinition import_policies = 1; + int64 default_import_policy = 2; + repeated PolicyDefinition export_policies = 3; + int64 default_export_policy = 4; +}
\ No newline at end of file diff --git a/gobgp/main.go b/gobgp/main.go index 9a89e4db..094a6dcc 100644 --- a/gobgp/main.go +++ b/gobgp/main.go @@ -236,6 +236,8 @@ func requestGrpc(cmd string, eArgs []string, remoteIP net.IP) error { return stateChangeNeighbor(CMD_ENABLE, remoteIP) case CMD_NEIGHBOR + "_" + CMD_DISABLE: return stateChangeNeighbor(CMD_DISABLE, remoteIP) + case CMD_NEIGHBOR + "_" + CMD_POLICY: + return showNeighborPolicy(remoteIP) case CMD_POLICY + "_" + CMD_PREFIX: if len(eArgs) == 0 { return showPolicyPrefixes() @@ -796,6 +798,7 @@ func (x *NeighborCommand) Execute(args []string) error { parser.AddCommand(CMD_SHUTDOWN, "subcommand for shutdown to neighbor", "", NewNeighborChangeStateCommand(eArgs[0], CMD_SHUTDOWN)) parser.AddCommand(CMD_ENABLE, "subcommand for enable to neighbor", "", NewNeighborChangeStateCommand(eArgs[0], CMD_ENABLE)) parser.AddCommand(CMD_DISABLE, "subcommand for disable to neighbor", "", NewNeighborChangeStateCommand(eArgs[0], CMD_DISABLE)) + parser.AddCommand(CMD_POLICY, "subcommand for policy of neighbor", "", NewNeighborPolicyCommand(eArgs[0])) if _, err := parser.ParseArgs(eArgs); err != nil { os.Exit(1) } @@ -1125,6 +1128,87 @@ func (x *NeighborChangeStateCommand) Execute(args []string) error { return nil } +type NeighborPolicyCommand struct { + remoteIP net.IP +} + +func NewNeighborPolicyCommand(addr string) *NeighborPolicyCommand { + return &NeighborPolicyCommand{ + remoteIP: net.ParseIP(addr), + } +} + +func showNeighborPolicy(remoteIP net.IP) error { + rt, err := checkAddressFamily(net.IP{}) + if err != nil { + return err + } + arg := &api.Arguments{ + Af: rt, + RouterId: remoteIP.String(), + } + + ap, e := client.GetNeighborPolicy(context.Background(), arg) + if e != nil { + return e + } + + if globalOpts.Json { + j, _ := json.Marshal(ap) + fmt.Println(string(j)) + return nil + } + var defaultInPolicy, defaultOutPolicy string + switch ap.DefaultImportPolicy { + case 0: + defaultInPolicy = "ACCEPT" + case 1: + defaultInPolicy = "REJECT" + } + switch ap.DefaultExportPolicy { + case 0: + defaultOutPolicy = "ACCEPT" + case 1: + defaultOutPolicy = "REJECT" + } + + fmt.Printf("DefaultImportPolicy: %s\n", defaultInPolicy) + fmt.Printf("DefaultImportPolicy: %s\n", defaultOutPolicy) + fmt.Printf("ImportPolicies:\n") + space := " " + for _, inPolicy := range ap.ImportPolicies { + fmt.Printf("%sPolicyName %s:\n", space, inPolicy.PolicyDefinitionName) + showPolicyStatement(space, inPolicy) + } + fmt.Printf("ExportPolicies:\n") + for _, outPolicy := range ap.ExportPolicies { + fmt.Printf("%sPolicyName %s:\n", space, outPolicy.PolicyDefinitionName) + showPolicyStatement(space, outPolicy) + } + return nil +} + +func (x *NeighborPolicyCommand) Execute(args []string) error { + eArgs := extractArgs(CMD_POLICY) + parser := flags.NewParser(nil, flags.Default) + if len(eArgs) == 0 { + if _, err := parser.ParseArgs(eArgs); err != nil { + os.Exit(1) + } + if err := requestGrpc(CMD_NEIGHBOR+"_"+CMD_POLICY, eArgs, x.remoteIP); err != nil { + return err + } + } else { + parser.Usage = "neighbor [ <neighbor address> ] policy \n gobgp neighbor [ <neighbor address> ]" + //parser.AddCommand(CMD_ADD, "subcommand for add policy to neighbor", "", &NeighborPolicyAddCommand{}) + //parser.AddCommand(CMD_DEL, "subcommand for delete policy from neighbor", "", &NeighborPolicyDelCommand{}) + if _, err := parser.ParseArgs(eArgs); err != nil { + os.Exit(1) + } + } + return nil +} + type PolicyCommand struct{} func (x *PolicyCommand) Execute(args []string) error { @@ -1543,12 +1627,12 @@ func (x *PolicyNeighborCommand) Execute(args []string) error { type PolicyRoutePolicyCommand struct{} -func showPolicyStatement(pd *api.PolicyDefinition) { +func showPolicyStatement(head string, pd *api.PolicyDefinition) { for _, st := range pd.StatementList { - fmt.Printf(" StatementName %s:\n", st.StatementNeme) - fmt.Println(" Conditions:") + fmt.Printf("%s StatementName %s:\n", head, st.StatementNeme) + fmt.Printf("%s Conditions:\n", head) prefixSet := st.Conditions.MatchPrefixSet - fmt.Print(" PrefixSet: ") + fmt.Printf("%s PrefixSet: ", head) if len(prefixSet.PrefixList) != 0 { format := formatPolicyPrefix([]*api.PrefixSet{st.Conditions.MatchPrefixSet}) for i, prefix := range prefixSet.PrefixList { @@ -1556,7 +1640,7 @@ func showPolicyStatement(pd *api.PolicyDefinition) { if i == 0 { fmt.Printf(format, prefixSet.PrefixSetName, p, prefix.MaskLengthRange) } else { - fmt.Print(" ") + fmt.Printf("%s ", head) fmt.Printf(format, "", p, prefix.MaskLengthRange) } @@ -1565,14 +1649,14 @@ func showPolicyStatement(pd *api.PolicyDefinition) { fmt.Print("\n") } neighborSet := st.Conditions.MatchNeighborSet - fmt.Print(" NeighborSet: ") + fmt.Printf("%s NeighborSet: ", head) if len(neighborSet.NeighborList) != 0 { format := formatPolicyNeighbor([]*api.NeighborSet{st.Conditions.MatchNeighborSet}) for i, neighbor := range neighborSet.NeighborList { if i == 0 { fmt.Printf(format, neighborSet.NeighborSetName, neighbor.Address) } else { - fmt.Print(" ") + fmt.Printf("%s ", head) fmt.Printf(format, "", neighbor.Address) } @@ -1589,13 +1673,13 @@ func showPolicyStatement(pd *api.PolicyDefinition) { case 2: option = "INVERT" } - fmt.Printf(" MatchOption: %s\n", option) - fmt.Println(" Actions:") + fmt.Printf("%s MatchOption: %s\n", head, option) + fmt.Printf("%s Actions:\n", head) action := "REJECT" if st.Actions.AcceptRoute { action = "ACCEPT" } - fmt.Printf(" %s\n", action) + fmt.Printf("%s %s\n", head, action) } } @@ -1632,10 +1716,10 @@ func showPolicyRoutePolicies() error { return nil } sort.Sort(m) - + space := "" for _, pd := range m { fmt.Printf("PolicyName %s:\n", pd.PolicyDefinitionName) - showPolicyStatement(pd) + showPolicyStatement(space, pd) } return nil } @@ -1655,8 +1739,9 @@ func showPolicyRoutePolicy(args []string) error { fmt.Println(string(j)) return nil } + space := "" fmt.Printf("PolicyName %s:\n", pd.PolicyDefinitionName) - showPolicyStatement(pd) + showPolicyStatement(space, pd) return nil } diff --git a/policy/policy.go b/policy/policy.go index 10ab0736..48b84c30 100644 --- a/policy/policy.go +++ b/policy/policy.go @@ -18,10 +18,12 @@ package policy import ( "fmt" log "github.com/Sirupsen/logrus" + "github.com/osrg/gobgp/api" "github.com/osrg/gobgp/config" "github.com/osrg/gobgp/packet" "github.com/osrg/gobgp/table" "net" + "reflect" "strconv" "strings" ) @@ -172,7 +174,8 @@ func (c *DefaultCondition) evaluate(path table.Path) bool { type PrefixCondition struct { DefaultCondition - PrefixList []Prefix + PrefixConditionName string + PrefixList []Prefix } func NewPrefixCondition(prefixSetName string, defPrefixList []config.PrefixSet) *PrefixCondition { @@ -196,7 +199,8 @@ func NewPrefixCondition(prefixSetName string, defPrefixList []config.PrefixSet) } pc := &PrefixCondition{ - PrefixList: prefixList, + PrefixConditionName: prefixSetName, + PrefixList: prefixList, } return pc @@ -224,7 +228,8 @@ func (c *PrefixCondition) evaluate(path table.Path) bool { type NeighborCondition struct { DefaultCondition - NeighborList []net.IP + NeighborConditionName string + NeighborList []net.IP } func NewNeighborCondition(neighborSetName string, defNeighborSetList []config.NeighborSet) *NeighborCondition { @@ -239,7 +244,8 @@ func NewNeighborCondition(neighborSetName string, defNeighborSetList []config.Ne } nc := &NeighborCondition{ - NeighborList: neighborList, + NeighborConditionName: neighborSetName, + NeighborList: neighborList, } return nc @@ -278,20 +284,20 @@ func NewAsPathLengthCondition(defAsPathLength config.AsPathLength) *AsPathLength var op AttributeComparison switch defAsPathLength.Operator { - case "eq": + case "eq": op = ATTRIBUTE_EQ - case "ge": + case "ge": op = ATTRIBUTE_GE - case "le": + case "le": op = ATTRIBUTE_LE - default: - return nil + default: + return nil } ac := &AsPathLengthCondition{ - Value: value, + Value: value, Operator: op, } @@ -305,16 +311,16 @@ func (c *AsPathLengthCondition) evaluate(path table.Path) bool { length := uint32(path.GetAsPathLen()) switch c.Operator { - case ATTRIBUTE_EQ: - return c.Value == length + case ATTRIBUTE_EQ: + return c.Value == length - case ATTRIBUTE_GE: - return c.Value <= length + case ATTRIBUTE_GE: + return c.Value <= length - case ATTRIBUTE_LE: - return c.Value >= length - default: - return false + case ATTRIBUTE_LE: + return c.Value >= length + default: + return false } } @@ -474,3 +480,73 @@ func ipPrefixCalculate(path table.Path, cPrefix Prefix) bool { } return false } + +func (p *Policy) ToApiStruct() *api.PolicyDefinition { + resStatements := make([]*api.Statement, 0) + for _, st := range p.Statements { + resPrefixSet := &api.PrefixSet{} + resNeighborSet := &api.NeighborSet{} + for _, condition := range st.Conditions { + switch reflect.TypeOf(condition) { + case reflect.TypeOf(&PrefixCondition{}): + prefixCondition := condition.(*PrefixCondition) + resPrefixList := make([]*api.Prefix, 0) + for _, prefix := range prefixCondition.PrefixList { + + resPrefix := &api.Prefix{ + Address: prefix.Address.String(), + MaskLength: uint32(prefix.Masklength), + } + if min, ok := prefix.MasklengthRange[MASK_LENGTH_RANGE_MIN]; ok { + if max, ok := prefix.MasklengthRange[MASK_LENGTH_RANGE_MAX]; ok { + resPrefix.MaskLengthRange = fmt.Sprintf("%d..%d", min, max) + } + } + + resPrefixList = append(resPrefixList, resPrefix) + } + resPrefixSet = &api.PrefixSet{ + PrefixSetName: prefixCondition.PrefixConditionName, + PrefixList: resPrefixList, + } + case reflect.TypeOf(&NeighborCondition{}): + neighborCondition := condition.(*NeighborCondition) + resNeighborList := make([]*api.Neighbor, 0) + for _, neighbor := range neighborCondition.NeighborList { + resNeighbor := &api.Neighbor{ + Address: neighbor.String(), + } + resNeighborList = append(resNeighborList, resNeighbor) + } + resNeighborSet = &api.NeighborSet{ + NeighborSetName: neighborCondition.NeighborConditionName, + NeighborList: resNeighborList, + } + } + } + resCondition := &api.Conditions{ + MatchPrefixSet: resPrefixSet, + MatchNeighborSet: resNeighborSet, + MatchSetOptions: int64(st.MatchSetOptions), + } + resAction := &api.Actions{ + AcceptRoute: false, + RejectRoute: true, + } + if st.Actions.(*RoutingActions).AcceptRoute { + resAction.AcceptRoute = true + resAction.RejectRoute = false + } + resStatement := &api.Statement{ + StatementNeme: st.Name, + Conditions: resCondition, + Actions: resAction, + } + resStatements = append(resStatements, resStatement) + } + + return &api.PolicyDefinition{ + PolicyDefinitionName: p.Name, + StatementList: resStatements, + } +} diff --git a/server/grpc_server.go b/server/grpc_server.go index 3fd45b83..d8188c56 100644 --- a/server/grpc_server.go +++ b/server/grpc_server.go @@ -40,6 +40,7 @@ const ( REQ_NEIGHBOR_SOFT_RESET_OUT REQ_NEIGHBOR_ENABLE REQ_NEIGHBOR_DISABLE + REQ_NEIGHBOR_POLICY REQ_GLOBAL_RIB REQ_GLOBAL_ADD REQ_GLOBAL_DELETE @@ -268,6 +269,24 @@ func (s *Server) ModPath(stream api.Grpc_ModPathServer) error { } } } + +func (s *Server) GetNeighborPolicy(ctx context.Context, arg *api.Arguments) (*api.ApplyPolicy, error) { + rf, err := convertAf2Rf(arg.Af) + if err != nil { + return nil, err + } + + req := NewGrpcRequest(REQ_NEIGHBOR_POLICY, arg.RouterId, rf, nil) + s.bgpServerCh <- req + + res := <-req.ResponseCh + if err := res.Err(); err != nil { + log.Debug(err.Error()) + return nil, err + } + return res.Data.(*api.ApplyPolicy), nil +} + 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 2a4e71a0..a89fb303 100644 --- a/server/peer.go +++ b/server/peer.go @@ -587,6 +587,47 @@ 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 + for _, conInPolicyName := range conInPolicyNames { + for _, inPolicy := range peer.importPolicies { + if conInPolicyName == inPolicy.Name { + resInPolicies = append(resInPolicies, inPolicy.ToApiStruct()) + } else { + resInPolicies = append(resInPolicies, &api.PolicyDefinition{PolicyDefinitionName: conInPolicyName}) + } + + } + } + // 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.peerConfig.ApplyPolicy.ExportPolicies + for _, conOutPolicyName := range conOutPolicyNames { + for _, outPolicy := range peer.exportPolicies { + if conOutPolicyName == outPolicy.Name { + resOutPolicies = append(resOutPolicies, outPolicy.ToApiStruct()) + } else { + resOutPolicies = append(resOutPolicies, &api.PolicyDefinition{PolicyDefinitionName: conOutPolicyName}) + } + + } + } + result.Data = &api.ApplyPolicy{ + DefaultImportPolicy: int64(peer.defaultImportPolicy), + ImportPolicies: resInPolicies, + DefaultExportPolicy: int64(peer.defaultExportPolicy), + ExportPolicies: resOutPolicies, + } + grpcReq.ResponseCh <- result + + close(grpcReq.ResponseCh) + return } grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) diff --git a/server/server.go b/server/server.go index d2a82a85..882bb147 100644 --- a/server/server.go +++ b/server/server.go @@ -296,7 +296,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { } } else { result = &GrpcResponse{ - ResponseErr: fmt.Errorf("Neighbor that has %v does not exist.", remoteAddr), + ResponseErr: fmt.Errorf("Neighbor that has %v doesn't exist.", remoteAddr), } } grpcReq.ResponseCh <- result @@ -310,8 +310,9 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { case REQ_LOCAL_RIB, REQ_NEIGHBOR_SHUTDOWN, REQ_NEIGHBOR_RESET, REQ_NEIGHBOR_SOFT_RESET, REQ_NEIGHBOR_SOFT_RESET_IN, REQ_NEIGHBOR_SOFT_RESET_OUT, REQ_ADJ_RIB_IN, REQ_ADJ_RIB_OUT, - REQ_NEIGHBOR_ENABLE, REQ_NEIGHBOR_DISABLE: - + REQ_NEIGHBOR_ENABLE, REQ_NEIGHBOR_DISABLE, + REQ_NEIGHBOR_POLICY: + log.Info("### in server") remoteAddr := grpcReq.RemoteAddr result := &GrpcResponse{} info, found := server.peerMap[remoteAddr] @@ -322,7 +323,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { } info.peer.serverMsgCh <- msg } else { - result.ResponseErr = fmt.Errorf("Neighbor that has %v does not exist.", remoteAddr) + result.ResponseErr = fmt.Errorf("Neighbor that has %v doesn't exist.", remoteAddr) grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) } @@ -338,7 +339,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { grpcReq.ResponseCh <- result } } else { - result.ResponseErr = fmt.Errorf("Prefix is not exist.") + result.ResponseErr = fmt.Errorf("Prefix doesn't exist.") grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) @@ -359,7 +360,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { } grpcReq.ResponseCh <- result } else { - result.ResponseErr = fmt.Errorf("Prefix that has %v does not exist.", name) + result.ResponseErr = fmt.Errorf("Prefix that has %v doesn't exist.", name) grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) @@ -373,6 +374,9 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) } + + // If the same PrefixSet is not set, add PrefixSet of request to the end. + // If only name of the PrefixSet is same, Overwrite with PrefixSet of request idxPrefixSet, idxPrefix := findPrefixSet(conPrefixSetList, prefixSet) if idxPrefixSet == -1 { conPrefixSetList = append(conPrefixSetList, prefixSet) @@ -391,13 +395,16 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { result := &GrpcResponse{} isReqPrefixSet, prefixSet := prefixToConfigStruct(reqPrefixSet) if isReqPrefixSet { + + // If only name of the PrefixSet is same, delete all of the elements of the PrefixSet. + // If the same PrefixSet is not set, delete the elements in PrefixSet. idxPrefixSet, idxPrefix := findPrefixSet(conPrefixSetList, prefixSet) if idxPrefixSet == -1 { - result.ResponseErr = fmt.Errorf("Prefix %v %v/%v %v does not exist.", prefixSet.PrefixSetName, + result.ResponseErr = fmt.Errorf("Prefix %v %v/%v %v doesn't exist.", prefixSet.PrefixSetName, prefixSet.PrefixList[0].Address, prefixSet.PrefixList[0].Masklength, prefixSet.PrefixList[0].MasklengthRange) } else { if idxPrefix == -1 { - result.ResponseErr = fmt.Errorf("Prefix %v %v/%v %v does not exist.", prefixSet.PrefixSetName, + result.ResponseErr = fmt.Errorf("Prefix %v %v/%v %v doesn't exist.", prefixSet.PrefixSetName, prefixSet.PrefixList[0].Address, prefixSet.PrefixList[0].Masklength, prefixSet.PrefixList[0].MasklengthRange) } else { copy(conPrefixSetList[idxPrefixSet].PrefixList[idxPrefix:], conPrefixSetList[idxPrefixSet].PrefixList[idxPrefix+1:]) @@ -413,7 +420,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { } } if idxPrefixSet == -1 { - result.ResponseErr = fmt.Errorf("Prefix %v does not exist.", prefixSet.PrefixSetName) + result.ResponseErr = fmt.Errorf("Prefix %v doesn't exist.", prefixSet.PrefixSetName) } else { copy(conPrefixSetList[idxPrefixSet:], conPrefixSetList[idxPrefixSet+1:]) conPrefixSetList = conPrefixSetList[:len(conPrefixSetList)-1] @@ -441,7 +448,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { grpcReq.ResponseCh <- result } } else { - result.ResponseErr = fmt.Errorf("Neighbor is not exist.") + result.ResponseErr = fmt.Errorf("Neighbor doesn't exist.") grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) @@ -462,7 +469,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { } grpcReq.ResponseCh <- result } else { - result.ResponseErr = fmt.Errorf("Neighbor that has %v does not exist.", name) + result.ResponseErr = fmt.Errorf("Neighbor that has %v doesn't exist.", name) grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) @@ -479,7 +486,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { grpcReq.ResponseCh <- result } } else { - result.ResponseErr = fmt.Errorf("Route Policy is not exist.") + result.ResponseErr = fmt.Errorf("Route Policy doesn't exist.") grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) @@ -501,13 +508,16 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { } grpcReq.ResponseCh <- result } else { - result.ResponseErr = fmt.Errorf("Route Policy that has %v does not exist.", name) + result.ResponseErr = fmt.Errorf("Route Policy that has %v doesn't exist.", name) grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) } } +// find PrefixSet of request from PrefixSet of configuration file. +// Return the idxPrefixSet of the location where the name of PrefixSet matches and, +// idxPrefix of the location where elements of PrefixSet matches func findPrefixSet(conPrefixSetList []config.PrefixSet, reqPrefixSet config.PrefixSet) (int, int) { idxPrefixSet := -1 idxPrefix := -1 @@ -529,6 +539,9 @@ func findPrefixSet(conPrefixSetList []config.PrefixSet, reqPrefixSet config.Pref return idxPrefixSet, idxPrefix } +// find NeighborSet of request from NeighborSet of configuration file. +// Return the idxNeighborSet of the location where the name of NeighborSet matches and, +// idxNeighbor of the location where elements of NeighborSet matches func findNeighborSet(conNeighborSetList []config.NeighborSet, reqNeighborSet config.NeighborSet) (int, int) { idxNeighborSet := -1 idxNeighbor := -1 |