diff options
author | Hitoshi Irino <irino@sfc.wide.ad.jp> | 2020-04-25 22:06:21 +0900 |
---|---|---|
committer | Hitoshi Irino <irino@sfc.wide.ad.jp> | 2020-04-26 07:43:50 +0900 |
commit | cf572bc5fc4647ea675c8a3482d80d181f62c66c (patch) | |
tree | b50bc6a682c801062ffe19a198b05ddc743a84ee /pkg | |
parent | f11b9c7afb198dd6bfea9a167f41a62569f24756 (diff) |
Refactoring Zebra and supporting frr7.3
- Change const values to camel case from snake case to make zero golint's warnigns
- Introduce convert functions (toEach, toCommon) between the newest version to older versions
- Merge code about decode nexthop to reduce lines of code
- Add frr7.3 statemet in allowable software for config
- Rename and Update generated file by stringer
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/server/server.go | 2 | ||||
-rw-r--r-- | pkg/server/zclient.go | 78 | ||||
-rw-r--r-- | pkg/server/zclient_test.go | 85 |
3 files changed, 68 insertions, 97 deletions
diff --git a/pkg/server/server.go b/pkg/server/server.go index a2c3dfbd..2ee49ec8 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -710,7 +710,7 @@ func (s *BgpServer) setPathVrfIdMap(paths []*table.Path, m map[uint32]bool) { } } default: - m[zebra.VRF_DEFAULT] = true + m[zebra.DefaultVrf] = true } } } diff --git a/pkg/server/zclient.go b/pkg/server/zclient.go index c002851a..5dac2a30 100644 --- a/pkg/server/zclient.go +++ b/pkg/server/zclient.go @@ -116,11 +116,10 @@ func filterOutExternalPath(paths []*table.Path) []*table.Path { return filteredPaths } -func addMessageLabelToIPRouteBody(path *table.Path, vrfId uint32, z *zebraClient, msgFlags *zebra.MESSAGE_FLAG, nexthop *zebra.Nexthop) { - v := z.client.Version +func addLabelToNexthop(path *table.Path, z *zebraClient, msgFlags *zebra.MessageFlag, nexthop *zebra.Nexthop) { rf := path.GetRouteFamily() - if v > 4 && (rf == bgp.RF_IPv4_VPN || rf == bgp.RF_IPv6_VPN) { - *msgFlags |= zebra.FRR_ZAPI5_MESSAGE_LABEL + if rf == bgp.RF_IPv4_VPN || rf == bgp.RF_IPv6_VPN { + z.client.SetLabelFlag(msgFlags, nexthop) switch rf { case bgp.RF_IPv4_VPN: for _, label := range path.GetNlri().(*bgp.LabeledVPNIPAddrPrefix).Labels.Labels { @@ -136,7 +135,7 @@ func addMessageLabelToIPRouteBody(path *table.Path, vrfId uint32, z *zebraClient } } -func newIPRouteBody(dst []*table.Path, vrfId uint32, z *zebraClient) (body *zebra.IPRouteBody, isWithdraw bool) { +func newIPRouteBody(dst []*table.Path, vrfID uint32, z *zebraClient) (body *zebra.IPRouteBody, isWithdraw bool) { version := z.client.Version paths := filterOutExternalPath(dst) if len(paths) == 0 { @@ -148,7 +147,7 @@ func newIPRouteBody(dst []*table.Path, vrfId uint32, z *zebraClient) (body *zebr var prefix net.IP var nexthop zebra.Nexthop nexthops := make([]zebra.Nexthop, 0, len(paths)) - msgFlags := zebra.MESSAGE_NEXTHOP + msgFlags := zebra.MessageNexthop switch path.GetRouteFamily() { case bgp.RF_IPv4_UC: prefix = path.GetNlri().(*bgp.IPAddrPrefix).IPAddrPrefixDefault.Prefix.To4() @@ -161,10 +160,10 @@ func newIPRouteBody(dst []*table.Path, vrfId uint32, z *zebraClient) (body *zebr default: return nil, false } - nhVrfId := uint32(zebra.VRF_DEFAULT) - for vrfPath, pathVrfId := range z.pathVrfMap { + nhVrfID := uint32(zebra.DefaultVrf) + for vrfPath, pathVrfID := range z.pathVrfMap { if path.Equal(vrfPath) { - nhVrfId = pathVrfId + nhVrfID = pathVrfID break } else { continue @@ -172,34 +171,27 @@ func newIPRouteBody(dst []*table.Path, vrfId uint32, z *zebraClient) (body *zebr } for _, p := range paths { nexthop.Gate = p.GetNexthop() - nexthop.VrfId = nhVrfId - if nhVrfId != vrfId { - addMessageLabelToIPRouteBody(path, vrfId, z, &msgFlags, &nexthop) + nexthop.VrfID = nhVrfID + if nhVrfID != vrfID { + addLabelToNexthop(path, z, &msgFlags, &nexthop) } nexthops = append(nexthops, nexthop) } plen, _ := strconv.ParseUint(l[1], 10, 8) med, err := path.GetMed() if err == nil { - if version < 5 { - msgFlags |= zebra.MESSAGE_METRIC - } else { - msgFlags |= zebra.FRR_ZAPI5_MESSAGE_METRIC - } + msgFlags |= zebra.MessageMetric.ToEach(version) } - var flags zebra.FLAG + var flags zebra.Flag if path.IsIBGP() { - flags = zebra.FLAG_IBGP | zebra.FLAG_ALLOW_RECURSION // 0x08|0x01 - if z.client.Version == 6 && z.client.SoftwareName != "frr6" { - flags = zebra.FRR_ZAPI6_FLAG_IBGP | zebra.FRR_ZAPI6_FLAG_ALLOW_RECURSION //0x04|0x01 - } + flags = zebra.FlagIBGP.ToEach(z.client.Version, z.client.SoftwareName) | zebra.FlagAllowRecursion } else if path.GetSource().MultihopTtl > 0 { - flags = zebra.FLAG_ALLOW_RECURSION // 0x01, FRR_ZAPI6_FLAG_ALLOW_RECURSION is same. + flags = zebra.FlagAllowRecursion // 0x01 } return &zebra.IPRouteBody{ - Type: zebra.ROUTE_BGP, + Type: zebra.RouteBGP, Flags: flags, - SAFI: zebra.SAFI_UNICAST, + Safi: zebra.SafiUnicast, Message: msgFlags, Prefix: zebra.Prefix{ Prefix: prefix, @@ -262,8 +254,8 @@ func newNexthopUnregisterBody(family uint16, prefix net.IP) *zebra.NexthopRegist func newPathFromIPRouteMessage(m *zebra.Message, version uint8, software string) *table.Path { header := m.Header body := m.Body.(*zebra.IPRouteBody) - family := body.RouteFamily(version) - isWithdraw := body.IsWithdraw(version) + family := body.RouteFamily(version, software) + isWithdraw := body.IsWithdraw(version, software) var nlri bgp.AddrPrefixInterface pattr := make([]bgp.PathAttributeInterface, 0) @@ -314,9 +306,9 @@ func newPathFromIPRouteMessage(m *zebra.Message, version uint8, software string) } type mplsLabelParameter struct { - rangeSize uint32 - maps map[uint64]*table.Bitmap - unassinedVrf []*table.Vrf //Vrfs which are not assigned MPLS label + rangeSize uint32 + maps map[uint64]*table.Bitmap + unassignedVrf []*table.Vrf //Vrfs which are not assigned MPLS label } type zebraClient struct { @@ -403,17 +395,27 @@ func (z *zebraClient) loop() { if len(paths) == 0 { // If there is no path bound for the given nexthop, send // NEXTHOP_UNREGISTER message. - z.client.SendNexthopRegister(msg.Header.VrfId, newNexthopUnregisterBody(uint16(body.Prefix.Family), body.Prefix.Prefix), true) + z.client.SendNexthopRegister(msg.Header.VrfID, newNexthopUnregisterBody(uint16(body.Prefix.Family), body.Prefix.Prefix), true) delete(z.nexthopCache, body.Prefix.Prefix.String()) } z.updatePathByNexthopCache(paths) case *zebra.GetLabelChunkBody: + log.WithFields(log.Fields{ + "Topic": "Zebra", + "Start": body.Start, + "End": body.End, + }).Debugf("zebra GetLabelChunkBody is received") startEnd := uint64(body.Start)<<32 | uint64(body.End) z.mplsLabel.maps[startEnd] = table.NewBitmap(int(body.End - body.Start + 1)) - for _, vrf := range z.mplsLabel.unassinedVrf { - z.assignAndSendVrfMplsLabel(vrf) + for _, vrf := range z.mplsLabel.unassignedVrf { + if err := z.assignAndSendVrfMplsLabel(vrf); err != nil { + log.WithFields(log.Fields{ + "Topic": "Zebra", + "Error": err, + }).Error("zebra failed to assign and send vrf mpls label") + } } - z.mplsLabel.unassinedVrf = nil + z.mplsLabel.unassignedVrf = nil } case ev := <-w.Event(): switch msg := ev.(type) { @@ -479,7 +481,7 @@ func newZebraClient(s *BgpServer, url string, protos []string, version uint8, nh ver++ } for elem, ver := range zapivers { - cli, err = zebra.NewClient(l[0], l[1], zebra.ROUTE_BGP, ver, softwareName) + cli, err = zebra.NewClient(l[0], l[1], zebra.RouteBGP, ver, softwareName, mplsLabelRangeSize) if cli != nil && err == nil { usingVersion = ver break @@ -511,7 +513,7 @@ func newZebraClient(s *BgpServer, url string, protos []string, version uint8, nh if err != nil { return nil, err } - cli.SendRedistribute(t, zebra.VRF_DEFAULT) + cli.SendRedistribute(t, zebra.DefaultVrf) } w := &zebraClient{ client: cli, @@ -525,7 +527,7 @@ func newZebraClient(s *BgpServer, url string, protos []string, version uint8, nh dead: make(chan struct{}), } go w.loop() - if mplsLabelRangeSize > 0 { + if mplsLabelRangeSize > 0 && cli.SupportMpls() { if err = cli.SendGetLabelChunk(&zebra.GetLabelChunkBody{ChunkSize: mplsLabelRangeSize}); err != nil { return nil, err } @@ -560,7 +562,7 @@ func (z *zebraClient) assignAndSendVrfMplsLabel(vrf *table.Vrf) error { return err } } else if vrf.MplsLabel == 0 { // GetLabelChunk is not performed - z.mplsLabel.unassinedVrf = append(z.mplsLabel.unassinedVrf, vrf) + z.mplsLabel.unassignedVrf = append(z.mplsLabel.unassignedVrf, vrf) } return err } diff --git a/pkg/server/zclient_test.go b/pkg/server/zclient_test.go index b9681e93..fbd189d6 100644 --- a/pkg/server/zclient_test.go +++ b/pkg/server/zclient_test.go @@ -28,65 +28,22 @@ import ( func Test_newPathFromIPRouteMessage(t *testing.T) { assert := assert.New(t) - - ipv4RouteAddCommand := map[uint8]zebra.API_TYPE{ - 2: zebra.IPV4_ROUTE_ADD, - 3: zebra.IPV4_ROUTE_ADD, - 4: zebra.FRR_IPV4_ROUTE_ADD, - 5: zebra.FRR_ZAPI5_IPV4_ROUTE_ADD, - 6: zebra.FRR_ZAPI6_ROUTE_ADD, - } - ipv4RouteDeleteCommand := map[uint8]zebra.API_TYPE{ - 2: zebra.IPV4_ROUTE_DELETE, - 3: zebra.IPV4_ROUTE_DELETE, - 4: zebra.FRR_IPV4_ROUTE_DELETE, - 5: zebra.FRR_ZAPI5_IPV4_ROUTE_DELETE, - 6: zebra.FRR_ZAPI6_ROUTE_DELETE, - } - ipv6RouteAddCommand := map[uint8]zebra.API_TYPE{ - 2: zebra.IPV6_ROUTE_ADD, - 3: zebra.IPV6_ROUTE_ADD, - 4: zebra.FRR_IPV6_ROUTE_ADD, - 5: zebra.FRR_ZAPI5_IPV6_ROUTE_ADD, - 6: zebra.FRR_ZAPI6_ROUTE_ADD, - } - ipv6RouteDeleteCommand := map[uint8]zebra.API_TYPE{ - 2: zebra.IPV6_ROUTE_DELETE, - 3: zebra.IPV6_ROUTE_DELETE, - 4: zebra.FRR_IPV6_ROUTE_DELETE, - 5: zebra.FRR_ZAPI5_IPV6_ROUTE_DELETE, - 6: zebra.FRR_ZAPI6_ROUTE_DELETE, - } - message := map[uint8]zebra.MESSAGE_FLAG{ - 2: zebra.MESSAGE_NEXTHOP | zebra.MESSAGE_DISTANCE | zebra.MESSAGE_METRIC | zebra.MESSAGE_MTU, - 3: zebra.MESSAGE_NEXTHOP | zebra.MESSAGE_DISTANCE | zebra.MESSAGE_METRIC | zebra.MESSAGE_MTU, - 4: zebra.FRR_MESSAGE_NEXTHOP | zebra.FRR_MESSAGE_DISTANCE | zebra.FRR_MESSAGE_METRIC | zebra.FRR_MESSAGE_MTU, - 5: zebra.FRR_ZAPI5_MESSAGE_NEXTHOP | zebra.FRR_ZAPI5_MESSAGE_DISTANCE | zebra.FRR_ZAPI5_MESSAGE_METRIC | zebra.FRR_ZAPI5_MESSAGE_MTU, - 6: zebra.FRR_ZAPI5_MESSAGE_NEXTHOP | zebra.FRR_ZAPI5_MESSAGE_DISTANCE | zebra.FRR_ZAPI5_MESSAGE_METRIC | zebra.FRR_ZAPI5_MESSAGE_MTU, - } - for v := zebra.MinZapiVer; v <= zebra.MaxZapiVer; v++ { // IPv4 Route Add m := &zebra.Message{} - marker := zebra.HEADER_MARKER - if v > 3 { - marker = zebra.FRR_HEADER_MARKER - } - flag := zebra.FLAG_SELECTED - if v > 5 { - flag = zebra.FRR_ZAPI6_FLAG_SELECTED - } + flag := zebra.FlagSelected.ToEach(v, "") + message := zebra.MessageNexthop | zebra.MessageDistance.ToEach(v) | zebra.MessageMetric.ToEach(v) | zebra.MessageMTU.ToEach(v) h := &zebra.Header{ Len: zebra.HeaderSize(v), - Marker: marker, + Marker: zebra.HeaderMarker(v), Version: v, - Command: ipv4RouteAddCommand[v], + Command: zebra.RouteAdd.ToEach(v, ""), } b := &zebra.IPRouteBody{ - Type: zebra.ROUTE_TYPE(zebra.ROUTE_STATIC), + Type: zebra.RouteType(zebra.RouteStatic), Flags: flag, - Message: message[v], - SAFI: zebra.SAFI(zebra.SAFI_UNICAST), // 1, FRR_ZAPI5_SAFI_UNICAST is same + Message: message, + Safi: zebra.Safi(zebra.SafiUnicast), // 1, FRR_ZAPI5_SAFI_UNICAST is same Prefix: zebra.Prefix{ Prefix: net.ParseIP("192.168.100.0"), PrefixLen: uint8(24), @@ -102,11 +59,11 @@ func Test_newPathFromIPRouteMessage(t *testing.T) { Distance: uint8(0), Metric: uint32(100), Mtu: uint32(0), - Api: zebra.API_TYPE(ipv4RouteAddCommand[v]), + API: zebra.APIType(zebra.RouteAdd.ToEach(v, "")), } m.Header = *h m.Body = b - + zebra.BackwardIPv6RouteDelete.ToEach(v, "") path := newPathFromIPRouteMessage(m, v, "") pp := table.NewPath(nil, path.GetNlri(), path.IsWithdraw, path.GetPathAttrs(), time.Now(), false) pp.SetIsFromExternal(path.IsFromExternal()) @@ -116,8 +73,8 @@ func Test_newPathFromIPRouteMessage(t *testing.T) { assert.False(pp.IsWithdraw) // IPv4 Route Delete - h.Command = ipv4RouteDeleteCommand[v] - b.Api = ipv4RouteDeleteCommand[v] + h.Command = zebra.RouteDelete.ToEach(v, "") + b.API = zebra.RouteDelete.ToEach(v, "") m.Header = *h m.Body = b @@ -132,8 +89,14 @@ func Test_newPathFromIPRouteMessage(t *testing.T) { assert.True(pp.IsWithdraw) // IPv6 Route Add - h.Command = ipv6RouteAddCommand[v] - b.Api = ipv6RouteAddCommand[v] + h.Command = zebra.RouteAdd.ToEach(v, "") + if v < 5 { + h.Command = zebra.BackwardIPv6RouteAdd.ToEach(v, "") + } + b.API = zebra.RouteAdd.ToEach(v, "") + if v < 5 { + b.API = zebra.BackwardIPv6RouteAdd.ToEach(v, "") + } b.Prefix.Prefix = net.ParseIP("2001:db8:0:f101::") b.Prefix.PrefixLen = uint8(64) b.Nexthops = []zebra.Nexthop{{Gate: net.ParseIP("::")}} @@ -151,8 +114,14 @@ func Test_newPathFromIPRouteMessage(t *testing.T) { assert.False(pp.IsWithdraw) // IPv6 Route Delete - h.Command = ipv6RouteDeleteCommand[v] - b.Api = ipv6RouteDeleteCommand[v] + h.Command = zebra.RouteDelete.ToEach(v, "") + if v < 5 { + h.Command = zebra.BackwardIPv6RouteDelete.ToEach(v, "") + } + b.API = zebra.RouteDelete.ToEach(v, "") + if v < 5 { + b.API = zebra.BackwardIPv6RouteDelete.ToEach(v, "") + } m.Header = *h m.Body = b |