From ed62f3f3d91e378c53d4f6faea4fad4280c3079e Mon Sep 17 00:00:00 2001 From: Ben Agricola Date: Mon, 11 Jul 2016 21:41:01 +0900 Subject: Add shorter prefix search Adds a shorter-prefixes search mode that finds any identical-or-less-specific routes than the input values. Bug-Url: #1006 Signed-off-by: Ben Agricola bagricola@squiz.co.uk --- api/gobgp.pb.go | 7 ++++--- api/gobgp.proto | 1 + docs/sources/cli-command-syntax.md | 4 ++-- gobgp/cmd/neighbor.go | 13 +++++++++---- server/server.go | 24 +++++++++++++++--------- 5 files changed, 31 insertions(+), 18 deletions(-) diff --git a/api/gobgp.pb.go b/api/gobgp.pb.go index 8edf8624..0021c995 100644 --- a/api/gobgp.pb.go +++ b/api/gobgp.pb.go @@ -1502,9 +1502,10 @@ func (*Path) ProtoMessage() {} func (*Path) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{92} } type Destination struct { - Prefix string `protobuf:"bytes,1,opt,name=prefix" json:"prefix,omitempty"` - Paths []*Path `protobuf:"bytes,2,rep,name=paths" json:"paths,omitempty"` - LongerPrefixes bool `protobuf:"varint,3,opt,name=longer_prefixes,json=longerPrefixes" json:"longer_prefixes,omitempty"` + Prefix string `protobuf:"bytes,1,opt,name=prefix" json:"prefix,omitempty"` + Paths []*Path `protobuf:"bytes,2,rep,name=paths" json:"paths,omitempty"` + LongerPrefixes bool `protobuf:"varint,3,opt,name=longer_prefixes,json=longerPrefixes" json:"longer_prefixes,omitempty"` + ShorterPrefixes bool `protobuf:"varint,4,opt,name=shorter_prefixes,json=shorterPrefixes" json:"shorter_prefixes,omitempty"` } func (m *Destination) Reset() { *m = Destination{} } diff --git a/api/gobgp.proto b/api/gobgp.proto index 8f6f87d6..71a30185 100644 --- a/api/gobgp.proto +++ b/api/gobgp.proto @@ -489,6 +489,7 @@ message Destination { string prefix = 1; repeated Path paths = 2; bool longer_prefixes = 3; + bool shorter_prefixes = 4; } message Table { diff --git a/docs/sources/cli-command-syntax.md b/docs/sources/cli-command-syntax.md index 2ebfec23..c7e359a2 100644 --- a/docs/sources/cli-command-syntax.md +++ b/docs/sources/cli-command-syntax.md @@ -40,7 +40,7 @@ gobgp has six subcommands. # show all Route information % gobgp global rib [-a
] # show a specific route information -% gobgp global rib [|] [longer-prefixes] [-a
] +% gobgp global rib [|] [longer-prefixes|shorter-prefixes] [-a
] ``` #### - example @@ -118,7 +118,7 @@ The following options can be specified in the global subcommand: # show all routes in [local|adj-in|adj-out] table % gobgp neighbor [local|adj-in|adj-out] [-a
] # show a specific route in [local|adj-in|adj-out] table -% gobgp neighbor [local|adj-in|adj-out] [|] [longer-prefixes] [-a
] +% gobgp neighbor [local|adj-in|adj-out] [|] [longer-prefixes|shorter-prefixes] [-a
] ``` #### - example diff --git a/gobgp/cmd/neighbor.go b/gobgp/cmd/neighbor.go index 9c93bbd2..bf687772 100644 --- a/gobgp/cmd/neighbor.go +++ b/gobgp/cmd/neighbor.go @@ -483,16 +483,21 @@ func showNeighborRib(r string, name string, args []string) error { return fmt.Errorf("route filtering is only supported for IPv4/IPv6 unicast routes") } longerPrefixes := false + shorterPrefixes := false if len(args) > 1 { - if args[1] != "longer-prefixes" { + if args[1] == "longer-prefixes" { + longerPrefixes = true + } else if args[1] == "shorter-prefixes" { + shorterPrefixes = true + } else { return fmt.Errorf("invalid format for route filtering") } - longerPrefixes = true } arg.Destinations = []*api.Destination{ &api.Destination{ - Prefix: args[0], - LongerPrefixes: longerPrefixes, + Prefix: args[0], + LongerPrefixes: longerPrefixes, + ShorterPrefixes: shorterPrefixes, }, } } diff --git a/server/server.go b/server/server.go index 6a766261..286ff85b 100644 --- a/server/server.go +++ b/server/server.go @@ -1605,7 +1605,21 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { } for _, dst := range arg.Table.Destinations { key := dst.Prefix - if _, err := f(id, key); err != nil { + if dst.LongerPrefixes { + _, prefix, _ := net.ParseCIDR(key) + for _, dst := range rib.Tables[af].GetLongerPrefixDestinations(prefix.String()) { + if d := dst.ToApiStruct(id); d != nil { + dsts = append(dsts, d) + } + } + } else if dst.ShorterPrefixes { + _, prefix, _ := net.ParseCIDR(key) + ones, bits := prefix.Mask.Size() + for i := ones; i > 0; i-- { + prefix.Mask = net.CIDRMask(i, bits) + f(id, prefix.String()) + } + } else if _, err := f(id, key); err != nil { if host := net.ParseIP(key); host != nil { masklen := 32 if af == bgp.RF_IPv6_UC { @@ -1617,14 +1631,6 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { } } } - } else if dst.LongerPrefixes { - _, prefix, _ := net.ParseCIDR(key) - - for _, dst := range rib.Tables[af].GetLongerPrefixDestinations(prefix.String()) { - if d := dst.ToApiStruct(id); d != nil { - dsts = append(dsts, d) - } - } } } } else { -- cgit v1.2.3