diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-08-20 13:42:38 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-08-20 13:42:38 +0900 |
commit | 859cedf4771364458b283a351b04326232d549b6 (patch) | |
tree | be3009701fbf7f68bd53bb3860a71504308c3a54 | |
parent | e6074318b7f94caeb31f90c7ee3489460cc781c4 (diff) |
server: fix for sorting adj-in/out routes
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | server/server.go | 25 | ||||
-rw-r--r-- | table/destination.go | 20 |
2 files changed, 32 insertions, 13 deletions
diff --git a/server/server.go b/server/server.go index 5f708ea2..cd4134da 100644 --- a/server/server.go +++ b/server/server.go @@ -1211,15 +1211,32 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { log.Debugf("RouteFamily=%v adj-rib-out found : %d", rf.String(), len(paths)) } - results := make([]*GrpcResponse, len(paths)) - for i, p := range paths { - result := &GrpcResponse{ + toResult := func(p *table.Path) *GrpcResponse { + return &GrpcResponse{ Data: &api.Destination{ Prefix: p.GetNlri().String(), Paths: []*api.Path{p.ToApiStruct()}, }, } - results[i] = result + } + + results := make([]*GrpcResponse, len(paths)) + if rf == bgp.RF_IPv4_UC { + r := radix.New() + for _, p := range paths { + r.Insert(table.CidrToRadixkey(p.GetNlri().String()), toResult(p)) + } + i := 0 + r.Walk(func(s string, v interface{}) bool { + r, _ := v.(*GrpcResponse) + results[i] = r + i++ + return false + }) + } else { + for i, p := range paths { + results[i] = toResult(p) + } } go sendMultipleResponses(grpcReq, results) diff --git a/table/destination.go b/table/destination.go index f669cced..55c90b53 100644 --- a/table/destination.go +++ b/table/destination.go @@ -41,6 +41,16 @@ const ( BPR_ROUTER_ID = "Router ID" ) +func CidrToRadixkey(cidr string) string { + _, n, _ := net.ParseCIDR(cidr) + ones, _ := n.Mask.Size() + var buffer bytes.Buffer + for i := 0; i < len(n.IP) && i < ones; i++ { + buffer.WriteString(fmt.Sprintf("%08b", n.IP[i])) + } + return buffer.String()[:ones] +} + type PeerInfo struct { AS uint32 ID net.IP @@ -84,15 +94,7 @@ func NewDestination(nlri bgp.AddrPrefixInterface) *Destination { newPathList: make([]*Path, 0), } if d.routeFamily == bgp.RF_IPv4_UC { - d.RadixKey = func(cidr string) string { - _, n, _ := net.ParseCIDR(cidr) - ones, _ := n.Mask.Size() - var buffer bytes.Buffer - for i := 0; i < len(n.IP) && i < ones; i++ { - buffer.WriteString(fmt.Sprintf("%08b", n.IP[i])) - } - return buffer.String()[:ones] - }(nlri.String()) + d.RadixKey = CidrToRadixkey(nlri.String()) } return d } |