diff --git a/net/ip.go b/net/ip.go index 2774ee5c16f0c029a89372d02743442837cccf79..6f0225c4e6cbed9a464b20444ba6b7d0e0d8900c 100644 --- a/net/ip.go +++ b/net/ip.go @@ -105,7 +105,7 @@ func (ip IP) Bytes() []byte { } func (ip IP) bytesIPv4() []byte { - u := ip.toUint32() + u := ip.ToUint32() return []byte{ byte(u & 0xFF000000 >> 24), byte(u & 0x00FF0000 >> 16), @@ -114,7 +114,8 @@ func (ip IP) bytesIPv4() []byte { } } -func (ip IP) toUint32() uint32 { +// ToUint32 return the rightmost 32 bits of an 'IP' +func (ip IP) ToUint32() uint32 { return uint32(^uint64(0) >> 32 & ip.lower) } @@ -158,7 +159,7 @@ func (ip IP) bitAtPositionIPv4(pos uint8) bool { return false } - return false + return (ip.ToUint32() & (1 << (32 - pos))) != 0 } func (ip IP) bitAtPositionIPv6(pos uint8) bool { diff --git a/net/prefix.go b/net/prefix.go index 914470b0cd34068d421022ce1b3ee131b0ccf4da..c40420247dfbfb11e8e05c01bae2fa1ed7d5e183 100644 --- a/net/prefix.go +++ b/net/prefix.go @@ -75,7 +75,7 @@ func (pfx Prefix) Contains(x Prefix) bool { func (pfx Prefix) containsIPv4(x Prefix) bool { mask := uint32((math.MaxUint32 << (32 - pfx.pfxlen))) - return (pfx.addr.toUint32() & mask) == (x.addr.toUint32() & mask) + return (pfx.addr.ToUint32() & mask) == (x.addr.ToUint32() & mask) } // Equal checks if pfx and x are equal @@ -94,8 +94,8 @@ func (pfx Prefix) GetSupernet(x Prefix) Prefix { func (pfx Prefix) supernetIPv4(x Prefix) Prefix { maxPfxLen := min(pfx.pfxlen, x.pfxlen) - 1 - a := pfx.addr.toUint32() >> (32 - maxPfxLen) - b := x.addr.toUint32() >> (32 - maxPfxLen) + a := pfx.addr.ToUint32() >> (32 - maxPfxLen) + b := x.addr.ToUint32() >> (32 - maxPfxLen) for i := 0; a != b; i++ { a = a >> 1 diff --git a/protocols/bgp/server/fsm_established.go b/protocols/bgp/server/fsm_established.go index 40694d339c2b7b263b2c8b5ce8496fb5106a6bd4..31a065e8e3703df3b518c7c7051e602133482f9e 100644 --- a/protocols/bgp/server/fsm_established.go +++ b/protocols/bgp/server/fsm_established.go @@ -207,14 +207,14 @@ func (s *establishedState) update(msg *packet.BGPMessage) (state, string) { func (s *establishedState) withdraws(u *packet.BGPUpdate) { for r := u.WithdrawnRoutes; r != nil; r = r.Next { - pfx := bnet.NewPfx(r.IP, r.Pfxlen) + pfx := bnet.NewPfx(bnet.IPv4(r.IP), r.Pfxlen) s.fsm.adjRIBIn.RemovePath(pfx, nil) } } func (s *establishedState) updates(u *packet.BGPUpdate) { for r := u.NLRI; r != nil; r = r.Next { - pfx := bnet.NewPfx(r.IP, r.Pfxlen) + pfx := bnet.NewPfx(bnet.IPv4(r.IP), r.Pfxlen) path := &route.Path{ Type: route.BGPPathType, diff --git a/protocols/bgp/server/update_sender.go b/protocols/bgp/server/update_sender.go index 078faebfde101b8d774a8785ad0a71c9dce3b159..6ba19536a1fba22fd214884b5bb15c8943e62dc0 100644 --- a/protocols/bgp/server/update_sender.go +++ b/protocols/bgp/server/update_sender.go @@ -133,7 +133,7 @@ func (u *UpdateSender) sendUpdates(pathAttrs *packet.PathAttribute, updatePrefix for _, pfx := range updatePrefix { nlri = &packet.NLRI{ PathIdentifier: pathID, - IP: pfx.Addr(), + IP: pfx.Addr().ToUint32(), Pfxlen: pfx.Pfxlen(), Next: update.NLRI, } diff --git a/protocols/bgp/server/withdraw.go b/protocols/bgp/server/withdraw.go index e3f1137e1e96469dfa69f3bd825a3a644a582fc2..98243560f929952ab7c26e47e3c797af17016772 100644 --- a/protocols/bgp/server/withdraw.go +++ b/protocols/bgp/server/withdraw.go @@ -21,13 +21,13 @@ func withDrawPrefixes(out io.Writer, opt *types.Options, prefixes ...net.Prefix) for _, pfx := range prefixes { if rootNLRI == nil { rootNLRI = &packet.NLRI{ - IP: pfx.Addr(), + IP: pfx.Addr().ToUint32(), Pfxlen: pfx.Pfxlen(), } currentNLRI = rootNLRI } else { currentNLRI.Next = &packet.NLRI{ - IP: pfx.Addr(), + IP: pfx.Addr().ToUint32(), Pfxlen: pfx.Pfxlen(), } currentNLRI = currentNLRI.Next @@ -52,7 +52,7 @@ func withDrawPrefixesAddPath(out io.Writer, opt *types.Options, pfx net.Prefix, update := &packet.BGPUpdate{ WithdrawnRoutes: &packet.NLRI{ PathIdentifier: p.BGPPath.PathIdentifier, - IP: pfx.Addr(), + IP: pfx.Addr().ToUint32(), Pfxlen: pfx.Pfxlen(), }, } diff --git a/protocols/bgp/server/withdraw_test.go b/protocols/bgp/server/withdraw_test.go index c21b6ada956e4c694a8408e962a67efe611574c2..cfe0a0283c0af493317284b495b150ae27f1edc8 100644 --- a/protocols/bgp/server/withdraw_test.go +++ b/protocols/bgp/server/withdraw_test.go @@ -23,7 +23,7 @@ func TestWithDrawPrefixes(t *testing.T) { }{ { Name: "One withdraw", - Prefix: []net.Prefix{net.NewPfx(1413010532, 24)}, + Prefix: []net.Prefix{net.NewPfx(net.IPv4(1413010532), 24)}, Expected: []byte{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // BGP Marker 0x00, 0x1b, // BGP Message Length @@ -37,7 +37,7 @@ func TestWithDrawPrefixes(t *testing.T) { }, { Name: "two withdraws", - Prefix: []net.Prefix{net.NewPfx(1413010532, 24), net.NewPfx(1413010534, 25)}, + Prefix: []net.Prefix{net.NewPfx(net.IPv4(1413010532), 24), net.NewPfx(net.IPv4(1413010534), 25)}, Expected: []byte{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // BGP Marker 0x00, 0x20, // BGP Message Length @@ -71,7 +71,7 @@ func TestWithDrawPrefixesAddPath(t *testing.T) { }{ { Name: "Normal withdraw", - Prefix: net.NewPfx(1413010532, 24), + Prefix: net.NewPfx(net.IPv4(1413010532), 24), Path: &route.Path{ Type: route.BGPPathType, BGPPath: &route.BGPPath{ @@ -92,7 +92,7 @@ func TestWithDrawPrefixesAddPath(t *testing.T) { }, { Name: "Non bgp withdraw", - Prefix: net.NewPfx(1413010532, 24), + Prefix: net.NewPfx(net.IPv4(1413010532), 24), Path: &route.Path{ Type: route.StaticPathType, }, @@ -101,7 +101,7 @@ func TestWithDrawPrefixesAddPath(t *testing.T) { }, { Name: "Nil BGPPathType", - Prefix: net.NewPfx(1413010532, 24), + Prefix: net.NewPfx(net.IPv4(1413010532), 24), Path: &route.Path{ Type: route.BGPPathType, }, diff --git a/route/route_test.go b/route/route_test.go index 74243fa1a3abbfd29f91dc621174071b69f9383a..4aaeb92804618918f8306364448c47521fa19cea 100644 --- a/route/route_test.go +++ b/route/route_test.go @@ -17,13 +17,13 @@ func TestNewRoute(t *testing.T) { }{ { name: "BGP Path", - pfx: bnet.NewPfx(strAddr("10.0.0.0"), 8), + pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8), path: &Path{ Type: BGPPathType, BGPPath: &BGPPath{}, }, expected: &Route{ - pfx: bnet.NewPfx(strAddr("10.0.0.0"), 8), + pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8), paths: []*Path{ &Path{ Type: BGPPathType, @@ -34,9 +34,9 @@ func TestNewRoute(t *testing.T) { }, { name: "Empty Path", - pfx: bnet.NewPfx(strAddr("10.0.0.0"), 8), + pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8), expected: &Route{ - pfx: bnet.NewPfx(strAddr("10.0.0.0"), 8), + pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8), paths: []*Path{}, }, }, @@ -57,9 +57,9 @@ func TestPrefix(t *testing.T) { { name: "Prefix", route: &Route{ - pfx: bnet.NewPfx(strAddr("10.0.0.0"), 8), + pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8), }, - expected: bnet.NewPfx(strAddr("10.0.0.0"), 8), + expected: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8), }, } @@ -73,14 +73,14 @@ func TestAddr(t *testing.T) { tests := []struct { name string route *Route - expected uint32 + expected bnet.IP }{ { name: "Prefix", route: &Route{ - pfx: bnet.NewPfx(strAddr("10.0.0.0"), 8), + pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8), }, - expected: 0xa000000, + expected: bnet.IPv4(0xa000000), }, } @@ -99,7 +99,7 @@ func TestPfxlen(t *testing.T) { { name: "Prefix", route: &Route{ - pfx: bnet.NewPfx(strAddr("10.0.0.0"), 8), + pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8), }, expected: 8, }, @@ -120,7 +120,7 @@ func TestAddPath(t *testing.T) { }{ { name: "Regular BGP path", - route: NewRoute(bnet.NewPfx(strAddr("10.0.0.0"), 8), &Path{ + route: NewRoute(bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8), &Path{ Type: BGPPathType, BGPPath: &BGPPath{}, }), @@ -129,7 +129,7 @@ func TestAddPath(t *testing.T) { BGPPath: &BGPPath{}, }, expected: &Route{ - pfx: bnet.NewPfx(strAddr("10.0.0.0"), 8), + pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8), paths: []*Path{ { Type: BGPPathType, @@ -144,13 +144,13 @@ func TestAddPath(t *testing.T) { }, { name: "Nil path", - route: NewRoute(bnet.NewPfx(strAddr("10.0.0.0"), 8), &Path{ + route: NewRoute(bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8), &Path{ Type: BGPPathType, BGPPath: &BGPPath{}, }), newPath: nil, expected: &Route{ - pfx: bnet.NewPfx(strAddr("10.0.0.0"), 8), + pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8), paths: []*Path{ { Type: BGPPathType, @@ -271,7 +271,7 @@ func TestCopy(t *testing.T) { { name: "", route: &Route{ - pfx: bnet.NewPfx(1000, 8), + pfx: bnet.NewPfx(bnet.IPv4(1000), 8), ecmpPaths: 2, paths: []*Path{ { @@ -280,7 +280,7 @@ func TestCopy(t *testing.T) { }, }, expected: &Route{ - pfx: bnet.NewPfx(1000, 8), + pfx: bnet.NewPfx(bnet.IPv4(1000), 8), ecmpPaths: 2, paths: []*Path{ { @@ -397,8 +397,3 @@ func TestECMPPaths(t *testing.T) { assert.Equal(t, tc.expected, tc.route.ECMPPaths()) } } - -func strAddr(s string) uint32 { - ret, _ := bnet.StrToAddr(s) - return ret -} diff --git a/routingtable/trie.go b/routingtable/trie.go index 51c1bfa01ea793087b4d2ee0fe9ae3f9ccb259d2..25a4262e6d4001383c5a2bd095b13d44fd47401b 100644 --- a/routingtable/trie.go +++ b/routingtable/trie.go @@ -14,10 +14,6 @@ type node struct { h *node } -func getBitUint32(x uint32, pos uint8) bool { - return ((x) & (1 << (32 - pos))) != 0 -} - func newNode(pfx net.Prefix, path *route.Path, skip uint8, dummy bool) *node { n := &node{ route: route.NewRoute(pfx, path),