diff --git a/pkg/config/oc/default.go b/pkg/config/oc/default.go index a61d312c4..25971075d 100644 --- a/pkg/config/oc/default.go +++ b/pkg/config/oc/default.go @@ -9,10 +9,10 @@ import ( "strconv" "github.com/osrg/gobgp/v3/internal/pkg/version" - "github.com/osrg/gobgp/v3/internal/pkg/zebra" "github.com/osrg/gobgp/v3/pkg/packet/bgp" "github.com/osrg/gobgp/v3/pkg/packet/bmp" "github.com/osrg/gobgp/v3/pkg/packet/rtr" + "github.com/osrg/gobgp/v3/pkg/zebra" "github.com/spf13/viper" ) diff --git a/pkg/server/server.go b/pkg/server/server.go index be1ef2619..44d7ca4cc 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -35,12 +35,12 @@ import ( api "github.com/osrg/gobgp/v3/api" "github.com/osrg/gobgp/v3/internal/pkg/table" "github.com/osrg/gobgp/v3/internal/pkg/version" - "github.com/osrg/gobgp/v3/internal/pkg/zebra" "github.com/osrg/gobgp/v3/pkg/apiutil" "github.com/osrg/gobgp/v3/pkg/config/oc" "github.com/osrg/gobgp/v3/pkg/log" "github.com/osrg/gobgp/v3/pkg/packet/bgp" "github.com/osrg/gobgp/v3/pkg/packet/bmp" + "github.com/osrg/gobgp/v3/pkg/zebra" ) type tcpListener struct { diff --git a/pkg/server/zclient.go b/pkg/server/zclient.go index 92527ae2e..e26857ff6 100644 --- a/pkg/server/zclient.go +++ b/pkg/server/zclient.go @@ -25,9 +25,9 @@ import ( "time" "github.com/osrg/gobgp/v3/internal/pkg/table" - "github.com/osrg/gobgp/v3/internal/pkg/zebra" "github.com/osrg/gobgp/v3/pkg/log" "github.com/osrg/gobgp/v3/pkg/packet/bgp" + "github.com/osrg/gobgp/v3/pkg/zebra" ) // nexthopStateCache stores a map of nexthop IP to metric value. Especially, diff --git a/pkg/server/zclient_test.go b/pkg/server/zclient_test.go index 9044028e6..f2e5a275e 100644 --- a/pkg/server/zclient_test.go +++ b/pkg/server/zclient_test.go @@ -23,8 +23,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/osrg/gobgp/v3/internal/pkg/table" - "github.com/osrg/gobgp/v3/internal/pkg/zebra" "github.com/osrg/gobgp/v3/pkg/log" + "github.com/osrg/gobgp/v3/pkg/zebra" ) func Test_newPathFromIPRouteMessage(t *testing.T) { diff --git a/internal/pkg/zebra/afi_string.go b/pkg/zebra/afi_string.go similarity index 100% rename from internal/pkg/zebra/afi_string.go rename to pkg/zebra/afi_string.go diff --git a/internal/pkg/zebra/apitype_string.go b/pkg/zebra/apitype_string.go similarity index 97% rename from internal/pkg/zebra/apitype_string.go rename to pkg/zebra/apitype_string.go index f8fffd594..8ab3852a6 100644 --- a/internal/pkg/zebra/apitype_string.go +++ b/pkg/zebra/apitype_string.go @@ -26,7 +26,7 @@ func _() { _ = x[routerIDAdd-15] _ = x[_routerIDDelete-16] _ = x[routerIDUpdate-17] - _ = x[hello-18] + _ = x[Hello-18] _ = x[_capabilities-19] _ = x[nexthopRegister-20] _ = x[nexthopUnregister-21] @@ -41,8 +41,8 @@ func _() { _ = x[_bfdDestDeregister-30] _ = x[_bfdDestUpdate-31] _ = x[_bfdDestReplay-32] - _ = x[redistributeRouteAdd-33] - _ = x[redistributeRouteDel-34] + _ = x[RedistributeRouteAdd-33] + _ = x[RedistributeRouteDel-34] _ = x[_vrfUnregister-35] _ = x[_vrfAdd-36] _ = x[_vrfDelete-37] @@ -260,7 +260,7 @@ func _() { _ = x[zapi3NexthopUpdate-29] } -const _APIType_name = "interfaceAddinterfaceDeleteinterfaceAddressAddinterfaceAddressDeleteinterfaceUpinterfaceDown_interfaceSetMaster_interfaceSetProtoDownRouteAddRouteDelete_routeNotifyOwnerredistributeAdd_redistributeDelete_redistributeDefaultAdd_redistributeDefaultDeleterouterIDAdd_routerIDDeleterouterIDUpdatehello_capabilitiesnexthopRegisternexthopUnregisternexthopUpdate_interfaceNBRAddressAdd_interfaceNBRAddressDelete_interfaceBFDDestUpdate_importRouteRegister_importRouteUnregister_importCheckUpdate_bfdDestRegister_bfdDestDeregister_bfdDestUpdate_bfdDestReplayredistributeRouteAddredistributeRouteDel_vrfUnregister_vrfAdd_vrfDeletevrfLabel_interfaceVRFUpdate_bfdClientRegister_bfdClientDeregister_interfaceEnableRADV_interfaceDisableRADVipv4NexthopLookupMRIB_interfaceLinkParams_mplsLabelsAdd_mplsLabelsDelete_mplsLabelsReplace_srPolicySet_srPolicyDelete_srPolicyNotifyStatus_ipmrRouteStatslabelManagerConnectlabelManagerConnectAsyncgetLabelChunkreleaseLabelChunk_fecRegister_fecUnregister_fecUpdate_advertiseDefaultGW_advertiseSviMACIP_advertiseSubnet_advertiseAllVNI_localESAdd_localESDel_remoteESVTEPAdd_remoteESVTEPDel_localESEVIAdd_localESEVIDel_vniAdd_vniDel_l3VNIAdd_l3VNIDel_remoteVTEPAdd_remoteVTEPDel_macIPAdd_macIPDel_ipPrefixRouteAdd_ipPrefixRouteDel_remoteMACIPAdd_remoteMACIPDel_duplicateAddrDetection_pwAdd_pwDelete_pwSet_pwUnset_pwStatusUpdate_ruleAdd_ruleDelete_ruleNotifyOwner_tableManagerConnect_getTableChunk_releaseTableChunk_ipSetCreate_ipSetDestroy_ipSetEntryAdd_ipSetEntryDelete_ipSetNotifyOwner_ipSetEntryNotifyOwner_ipTableAdd_ipTableDelete_ipTableNotifyOwner_vxlanFloodControl_vxlanSgAdd_vxlanSgDel_vxlanSgReplay_mlagProcessUp_mlagProcessDown_mlagClientRegister_mlagClientUnregister_mlagClientForwardMsg_nhgAdd_nhgDel_nhgNotifyOwner_nhgEvpnRemoteNhAdd_nhgEvpnRemoteNhDel_srv6LocatorAdd_srv6LocatorDelete_srv6ManagerGetLocatorChunk_srv6ManagerReleaseLocatorChunkzebraError_clientCapabilities_opaqueMessage_opaqueRegister_opaqueUnregister_neighDiscover_RouteNotifyRequest_ClientCloseNotify_NhrpNeighAdded_NhrpNeighRemoved_NhrpNeighGet_NhrpNeighRegister_NhrpNeighUnregister_NeighIPAdd_NeighIPDel_ConfigureArp_GreGet_GreUpdate_GreSourceSetBackwardIPv6RouteAddBackwardIPv6RouteDelete" +const _APIType_name = "interfaceAddinterfaceDeleteinterfaceAddressAddinterfaceAddressDeleteinterfaceUpinterfaceDown_interfaceSetMaster_interfaceSetProtoDownRouteAddRouteDelete_routeNotifyOwnerredistributeAdd_redistributeDelete_redistributeDefaultAdd_redistributeDefaultDeleterouterIDAdd_routerIDDeleterouterIDUpdateHello_capabilitiesnexthopRegisternexthopUnregisternexthopUpdate_interfaceNBRAddressAdd_interfaceNBRAddressDelete_interfaceBFDDestUpdate_importRouteRegister_importRouteUnregister_importCheckUpdate_bfdDestRegister_bfdDestDeregister_bfdDestUpdate_bfdDestReplayRedistributeRouteAddRedistributeRouteDel_vrfUnregister_vrfAdd_vrfDeletevrfLabel_interfaceVRFUpdate_bfdClientRegister_bfdClientDeregister_interfaceEnableRADV_interfaceDisableRADVipv4NexthopLookupMRIB_interfaceLinkParams_mplsLabelsAdd_mplsLabelsDelete_mplsLabelsReplace_srPolicySet_srPolicyDelete_srPolicyNotifyStatus_ipmrRouteStatslabelManagerConnectlabelManagerConnectAsyncgetLabelChunkreleaseLabelChunk_fecRegister_fecUnregister_fecUpdate_advertiseDefaultGW_advertiseSviMACIP_advertiseSubnet_advertiseAllVNI_localESAdd_localESDel_remoteESVTEPAdd_remoteESVTEPDel_localESEVIAdd_localESEVIDel_vniAdd_vniDel_l3VNIAdd_l3VNIDel_remoteVTEPAdd_remoteVTEPDel_macIPAdd_macIPDel_ipPrefixRouteAdd_ipPrefixRouteDel_remoteMACIPAdd_remoteMACIPDel_duplicateAddrDetection_pwAdd_pwDelete_pwSet_pwUnset_pwStatusUpdate_ruleAdd_ruleDelete_ruleNotifyOwner_tableManagerConnect_getTableChunk_releaseTableChunk_ipSetCreate_ipSetDestroy_ipSetEntryAdd_ipSetEntryDelete_ipSetNotifyOwner_ipSetEntryNotifyOwner_ipTableAdd_ipTableDelete_ipTableNotifyOwner_vxlanFloodControl_vxlanSgAdd_vxlanSgDel_vxlanSgReplay_mlagProcessUp_mlagProcessDown_mlagClientRegister_mlagClientUnregister_mlagClientForwardMsg_nhgAdd_nhgDel_nhgNotifyOwner_nhgEvpnRemoteNhAdd_nhgEvpnRemoteNhDel_srv6LocatorAdd_srv6LocatorDelete_srv6ManagerGetLocatorChunk_srv6ManagerReleaseLocatorChunkzebraError_clientCapabilities_opaqueMessage_opaqueRegister_opaqueUnregister_neighDiscover_RouteNotifyRequest_ClientCloseNotify_NhrpNeighAdded_NhrpNeighRemoved_NhrpNeighGet_NhrpNeighRegister_NhrpNeighUnregister_NeighIPAdd_NeighIPDel_ConfigureArp_GreGet_GreUpdate_GreSourceSetBackwardIPv6RouteAddBackwardIPv6RouteDelete" var _APIType_index = [...]uint16{0, 12, 27, 46, 68, 79, 92, 111, 133, 141, 152, 169, 184, 203, 226, 252, 263, 278, 292, 297, 310, 325, 342, 355, 378, 404, 427, 447, 469, 487, 503, 521, 535, 549, 569, 589, 603, 610, 620, 628, 647, 665, 685, 705, 726, 747, 767, 781, 798, 816, 828, 843, 864, 879, 898, 922, 935, 952, 964, 978, 988, 1007, 1025, 1041, 1057, 1068, 1079, 1095, 1111, 1125, 1139, 1146, 1153, 1162, 1171, 1185, 1199, 1208, 1217, 1234, 1251, 1266, 1281, 1304, 1310, 1319, 1325, 1333, 1348, 1356, 1367, 1383, 1403, 1417, 1435, 1447, 1460, 1474, 1491, 1508, 1530, 1541, 1555, 1574, 1592, 1603, 1614, 1628, 1642, 1658, 1677, 1698, 1719, 1726, 1733, 1748, 1767, 1786, 1801, 1819, 1846, 1877, 1887, 1906, 1920, 1935, 1952, 1966, 1985, 2003, 2018, 2035, 2048, 2066, 2086, 2097, 2108, 2121, 2128, 2138, 2151, 2171, 2194} diff --git a/internal/pkg/zebra/linktype_string.go b/pkg/zebra/linktype_string.go similarity index 100% rename from internal/pkg/zebra/linktype_string.go rename to pkg/zebra/linktype_string.go diff --git a/internal/pkg/zebra/lsptype_string.go b/pkg/zebra/lsptype_string.go similarity index 100% rename from internal/pkg/zebra/lsptype_string.go rename to pkg/zebra/lsptype_string.go diff --git a/internal/pkg/zebra/nexthopflag_string.go b/pkg/zebra/nexthopflag_string.go similarity index 100% rename from internal/pkg/zebra/nexthopflag_string.go rename to pkg/zebra/nexthopflag_string.go diff --git a/internal/pkg/zebra/nexthoptype_string.go b/pkg/zebra/nexthoptype_string.go similarity index 100% rename from internal/pkg/zebra/nexthoptype_string.go rename to pkg/zebra/nexthoptype_string.go diff --git a/internal/pkg/zebra/ptmenable_string.go b/pkg/zebra/ptmenable_string.go similarity index 100% rename from internal/pkg/zebra/ptmenable_string.go rename to pkg/zebra/ptmenable_string.go diff --git a/internal/pkg/zebra/ptmstatus_string.go b/pkg/zebra/ptmstatus_string.go similarity index 100% rename from internal/pkg/zebra/ptmstatus_string.go rename to pkg/zebra/ptmstatus_string.go diff --git a/internal/pkg/zebra/routetype_string.go b/pkg/zebra/routetype_string.go similarity index 100% rename from internal/pkg/zebra/routetype_string.go rename to pkg/zebra/routetype_string.go diff --git a/internal/pkg/zebra/safi_string.go b/pkg/zebra/safi_string.go similarity index 100% rename from internal/pkg/zebra/safi_string.go rename to pkg/zebra/safi_string.go diff --git a/internal/pkg/zebra/zapi.go b/pkg/zebra/zapi.go similarity index 96% rename from internal/pkg/zebra/zapi.go rename to pkg/zebra/zapi.go index 1ed72e4ea..73fc15bae 100644 --- a/internal/pkg/zebra/zapi.go +++ b/pkg/zebra/zapi.go @@ -40,6 +40,10 @@ const ( DefaultVrf = 0 ) +var ( + MaxSoftware = NewSoftware(MaxZapiVer, "frr8.2") +) + const ( headerMarker uint8 = 255 frrHeaderMarker uint8 = 254 @@ -268,7 +272,7 @@ const ( routerIDAdd _routerIDDelete routerIDUpdate - hello + Hello _capabilities // added in frr5 nexthopRegister // 20 nexthopUnregister @@ -283,8 +287,8 @@ const ( _bfdDestDeregister // 30 _bfdDestUpdate _bfdDestReplay - redistributeRouteAdd // 33 // 30 in frr8.2 - redistributeRouteDel + RedistributeRouteAdd // 33 // 30 in frr8.2 + RedistributeRouteDel _vrfUnregister _vrfAdd _vrfDelete @@ -438,8 +442,8 @@ const ( ) var apiTypeZapi6Frr8dot2Map = map[APIType]APIType{ // frr8.2 - redistributeRouteAdd: zapi6Frr8dot2RedistributeRouteAdd, - redistributeRouteDel: zapi6Frr8dot2RedistributeRouteDel, + RedistributeRouteAdd: zapi6Frr8dot2RedistributeRouteAdd, + RedistributeRouteDel: zapi6Frr8dot2RedistributeRouteDel, vrfLabel: zapi6Frr8dot2VrfLabel, ipv4NexthopLookupMRIB: zapi6Frr8dot2Ipv4NexthopLookupMRIB, labelManagerConnect: zapi6Frr8dot2LabelManagerConnect, @@ -502,12 +506,12 @@ var apiTypeZapi6Frr7Map = map[APIType]APIType{ // frr7.0, 7.1 redistributeAdd: zapi6Frr7RedistributAdd, routerIDAdd: zapi6Frr7RouterIDAdd, routerIDUpdate: zapi6Frr7RouterIDUpdate, - hello: zapi6Frr7Hello, + Hello: zapi6Frr7Hello, nexthopRegister: zapi6Frr7NexthopRegister, nexthopUnregister: zapi6Frr7NexthopUnregister, nexthopUpdate: zapi6Frr7NexthopUpdate, - redistributeRouteAdd: zapi6Frr7RedistributeRouteAdd, - redistributeRouteDel: zapi6Frr7RedistributeRouteDel, + RedistributeRouteAdd: zapi6Frr7RedistributeRouteAdd, + RedistributeRouteDel: zapi6Frr7RedistributeRouteDel, vrfLabel: zapi6Frr7VrfLabel, ipv4NexthopLookupMRIB: zapi6Frr7Ipv4NexthopLookupMRIB, labelManagerConnect: zapi6Frr7LabelManagerConnect, @@ -522,12 +526,12 @@ var apiTypeZapi6Frr6Map = map[APIType]APIType{ redistributeAdd: zapi6Frr7RedistributAdd, // same as frr7.0&7.1 routerIDAdd: zapi6Frr7RouterIDAdd, // same as frr7.0&7.1 routerIDUpdate: zapi6Frr7RouterIDUpdate, // same as frr7.0&7.1 - hello: zapi6Frr7Hello, // same as frr7.0&7.1 + Hello: zapi6Frr7Hello, // same as frr7.0&7.1 nexthopRegister: zapi6Frr7NexthopRegister, // same as frr7.0&7.1 nexthopUnregister: zapi6Frr7NexthopUnregister, // same as frr7.0&7.1 nexthopUpdate: zapi6Frr7NexthopUpdate, // same as frr7.0&7.1 - redistributeRouteAdd: redistributeRouteAdd, // same as frr7.2&7.3 - redistributeRouteDel: redistributeRouteDel, // same as frr7.2&7.3 + RedistributeRouteAdd: RedistributeRouteAdd, // same as frr7.2&7.3 + RedistributeRouteDel: RedistributeRouteDel, // same as frr7.2&7.3 vrfLabel: vrfLabel, // same as frr7.2&7.3 ipv4NexthopLookupMRIB: ipv4NexthopLookupMRIB, // same as frr7.2&7.3 labelManagerConnect: zapi6Frr7dot2LabelManagerConnect, // same as frr7.2 @@ -547,8 +551,8 @@ var apiTypeZapi5ClMap = map[APIType]APIType{ nexthopRegister: zapi6Frr7NexthopRegister, // same as frr7.0&7.1 nexthopUnregister: zapi6Frr7NexthopUnregister, // same as frr7.0&7.1 nexthopUpdate: zapi6Frr7NexthopUpdate, // same as frr7.0&7.1 - redistributeRouteAdd: zapi6Frr7RedistributeRouteAdd, // same as frr7.0&7.1 - redistributeRouteDel: zapi6Frr7RedistributeRouteDel, // same as frr7.0&7.1 + RedistributeRouteAdd: zapi6Frr7RedistributeRouteAdd, // same as frr7.0&7.1 + RedistributeRouteDel: zapi6Frr7RedistributeRouteDel, // same as frr7.0&7.1 vrfLabel: zapi6Frr7VrfLabel, // same as frr7.0&7.1 labelManagerConnect: zapi5ClLabelManagerConnect, getLabelChunk: zapi5ClGetLabelChunk, @@ -579,12 +583,12 @@ var apiTypeZapi5Frr5Map = map[APIType]APIType{ redistributeAdd: zapi5RedistributAdd, routerIDAdd: zapi5RouterIDAdd, routerIDUpdate: zapi5RouterIDUpdate, - hello: zapi5Hello, + Hello: zapi5Hello, nexthopRegister: zapi5Frr5NexthopRegister, nexthopUnregister: zapi5Frr5NexthopUnregister, nexthopUpdate: zapi5Frr5NexthopUpdate, - redistributeRouteAdd: zapi5Frr5RedistributeRouteAdd, - redistributeRouteDel: zapi5Frr5RedistributeRouteDel, + RedistributeRouteAdd: zapi5Frr5RedistributeRouteAdd, + RedistributeRouteDel: zapi5Frr5RedistributeRouteDel, vrfLabel: zapi5Frr5VrfLabel, ipv4NexthopLookupMRIB: zapi5Frr5Ipv4NexthopLookupMRIB, labelManagerConnect: zapi5Frr5LabelManagerConnect, @@ -611,12 +615,12 @@ var apiTypeZapi5Frr4Map = map[APIType]APIType{ redistributeAdd: zapi5RedistributAdd, routerIDAdd: zapi5RouterIDAdd, routerIDUpdate: zapi5RouterIDUpdate, - hello: zapi5Hello, + Hello: zapi5Hello, nexthopRegister: zapi5Frr4NexthopRegister, nexthopUnregister: zapi5Frr4NexthopUnregister, nexthopUpdate: zapi5Frr4NexthopUpdate, - redistributeRouteAdd: zapi5Frr4RedistributeRouteAdd, - redistributeRouteDel: zapi5Frr4RedistributeRouteDel, + RedistributeRouteAdd: zapi5Frr4RedistributeRouteAdd, + RedistributeRouteDel: zapi5Frr4RedistributeRouteDel, ipv4NexthopLookupMRIB: zapi5Frr4Ipv4NexthopLookupMRIB, labelManagerConnect: zapi5Frr4LabelManagerConnect, getLabelChunk: zapi5Frr4GetLabelChunk, @@ -650,12 +654,12 @@ var apiTypeZapi4Map = map[APIType]APIType{ redistributeAdd: zapi4RedistributAdd, routerIDAdd: zapi4RouterIDAdd, routerIDUpdate: zapi4RouterIDUpdate, - hello: zapi4Hello, + Hello: zapi4Hello, nexthopRegister: zapi4NexthopRegister, nexthopUnregister: zapi4NexthopUnregister, nexthopUpdate: zapi4NexthopUpdate, - redistributeRouteAdd: zapi4RedistributeIPv4Add, // deleted in zapi5 - redistributeRouteDel: zapi4RedistributeIPv4Del, // deleted in zapi5 + RedistributeRouteAdd: zapi4RedistributeIPv4Add, // deleted in zapi5 + RedistributeRouteDel: zapi4RedistributeIPv4Del, // deleted in zapi5 ipv4NexthopLookupMRIB: zapi6Frr7Ipv4NexthopLookupMRIB, // same as frr7.0&7.1 labelManagerConnect: zapi4LabelManagerConnect, getLabelChunk: zapi4GetLabelChunk, @@ -700,7 +704,7 @@ var apiTypeZapi3Map = map[APIType]APIType{ redistributeAdd: zapi3RedistributeAdd, routerIDAdd: zapi3RouterIDAdd, routerIDUpdate: zapi3RouterIDUpdate, - hello: zapi3Hello, + Hello: zapi3Hello, nexthopRegister: zapi3NexthopRegister, nexthopUnregister: zapi3NexthopUnregister, nexthopUpdate: zapi3NexthopUpdate, @@ -752,7 +756,7 @@ func (t APIType) ToEach(version uint8, software Software) APIType { } return backward // success to convert } -func (t APIType) toCommon(version uint8, software Software) APIType { +func (t APIType) ToCommon(version uint8, software Software) APIType { if !t.doesNeedConversion(version, software) { return t } @@ -1354,6 +1358,63 @@ type Client struct { logger log.Logger } +func ReceiveSingleMsg(logger log.Logger, conn net.Conn, version uint8, software Software, topic string) (*Message, error) { + headerBuf, err := readAll(conn, int(HeaderSize(version))) + if err != nil { + logger.Error("failed to read header", + log.Fields{ + "Topic": topic, + "Error": err}) + return nil, err + } + + hd := &Header{} + err = hd.decodeFromBytes(headerBuf) + if version != hd.Version { + logger.Warn(fmt.Sprintf("ZAPI version mismatch. configured version: %d, version of received message:%d", version, hd.Version), + log.Fields{ + "Topic": topic}) + return nil, errors.New("ZAPI version mismatch") + } + if err != nil { + logger.Error("failed to decode header", + log.Fields{ + "Topic": topic, + "Data": headerBuf, + "Error": err}) + return nil, err + } + + bodyBuf, err := readAll(conn, int(hd.Len-HeaderSize(version))) + if err != nil { + logger.Error("failed to read body", + log.Fields{ + "Topic": topic, + "Header": hd, + "Error": err}) + return nil, err + } + + m, err := parseMessage(hd, bodyBuf, software) + if err != nil { + // Just outputting warnings (not error message) and ignore this + // error considering the case that body parser is not implemented yet. + logger.Warn("failed to decode body", + log.Fields{ + "Topic": topic, + "Header": hd, + "Data": bodyBuf, + "Error": err}) + return nil, nil + } + logger.Debug("read message from zebra", + log.Fields{ + "Topic": topic, + "Message": m}) + + return m, nil +} + // NewClient returns a Client instance (Client constructor) func NewClient(logger log.Logger, network, address string, typ RouteType, version uint8, software Software, mplsLabelRangeSize uint32) (*Client, error) { conn, err := net.Dial(network, address) @@ -1381,7 +1442,7 @@ func NewClient(logger log.Logger, network, address string, typ RouteType, versio for { m, more := <-outgoing if more { - b, err := m.serialize(software) + b, err := m.Serialize(software) if err != nil { logger.Warn(fmt.Sprintf("failed to serialize: %v", m), log.Fields{ @@ -1414,65 +1475,8 @@ func NewClient(logger log.Logger, network, address string, typ RouteType, versio c.sendLabelManagerConnect(true) } - receiveSingleMsg := func() (*Message, error) { - headerBuf, err := readAll(conn, int(HeaderSize(version))) - if err != nil { - logger.Error("failed to read header", - log.Fields{ - "Topic": "Zebra", - "Error": err}) - return nil, err - } - - hd := &Header{} - err = hd.decodeFromBytes(headerBuf) - if c.Version != hd.Version { - logger.Warn(fmt.Sprintf("ZAPI version mismatch. configured version: %d, version of received message:%d", c.Version, hd.Version), - log.Fields{ - "Topic": "Zebra"}) - return nil, errors.New("ZAPI version mismatch") - } - if err != nil { - logger.Error("failed to decode header", - log.Fields{ - "Topic": "Zebra", - "Data": headerBuf, - "Error": err}) - return nil, err - } - - bodyBuf, err := readAll(conn, int(hd.Len-HeaderSize(version))) - if err != nil { - logger.Error("failed to read body", - log.Fields{ - "Topic": "Zebra", - "Header": hd, - "Error": err}) - return nil, err - } - - m, err := parseMessage(hd, bodyBuf, software) - if err != nil { - // Just outputting warnings (not error message) and ignore this - // error considering the case that body parser is not implemented yet. - logger.Warn("failed to decode body", - log.Fields{ - "Topic": "Zebra", - "Header": hd, - "Data": bodyBuf, - "Error": err}) - return nil, nil - } - logger.Debug("read message from zebra", - log.Fields{ - "Topic": "Zebra", - "Message": m}) - - return m, nil - } - // Try to receive the first message from Zebra. - if m, err := receiveSingleMsg(); err != nil { + if m, err := ReceiveSingleMsg(logger, conn, version, software, "Zebra"); err != nil { c.close() // Return error explicitly in order to retry connection. return nil, err @@ -1484,7 +1488,7 @@ func NewClient(logger log.Logger, network, address string, typ RouteType, versio go func() { defer close(incoming) for { - if m, err := receiveSingleMsg(); err != nil { + if m, err := ReceiveSingleMsg(logger, conn, version, software, "Zebra"); err != nil { return } else if m != nil { incoming <- m @@ -1541,11 +1545,11 @@ func (c *Client) sendCommand(command APIType, vrfID uint32, body Body) error { // SendHello sends HELLO message to zebra daemon. func (c *Client) SendHello() error { if c.redistDefault > 0 { - body := &helloBody{ + body := &HelloBody{ redistDefault: c.redistDefault, instance: 0, } - return c.sendCommand(hello, DefaultVrf, body) + return c.sendCommand(Hello, DefaultVrf, body) } return nil } @@ -1785,7 +1789,7 @@ func (b *unknownBody) string(version uint8, software Software) string { return fmt.Sprintf("data: %v", b.Data) } -type helloBody struct { +type HelloBody struct { redistDefault RouteType instance uint16 sessionID uint32 // frr7.4, 7.5, 8, 8.1, 8.2 @@ -1795,7 +1799,7 @@ type helloBody struct { // Ref: zread_hello in zebra/zserv.c of Quagga1.2&FRR3 (ZAPI3&4) // Ref: zread_hello in zebra/zapi_msg.c of FRR5&FRR6&FRR7&FRR7.1&FRR7.2&FRR7.3&FRR7.4&FRR7.5&FRR8 (ZAPI5&6) -func (b *helloBody) decodeFromBytes(data []byte, version uint8, software Software) error { +func (b *HelloBody) decodeFromBytes(data []byte, version uint8, software Software) error { b.redistDefault = RouteType(data[0]) if version > 3 { //frr b.instance = binary.BigEndian.Uint16(data[1:3]) @@ -1812,7 +1816,7 @@ func (b *helloBody) decodeFromBytes(data []byte, version uint8, software Softwar // Ref: zebra_hello_send in lib/zclient.c of Quagga1.2&FRR3&FRR5&FRR6&FRR7&FRR7.1&FRR7.2&FRR7.3 (ZAPI3&4&5&6) // Ref: zclient_send_hello in lib/zclient.c of FRR7.4&FRR7.5&FRR8 (ZAPI6) -func (b *helloBody) serialize(version uint8, software Software) ([]byte, error) { +func (b *HelloBody) serialize(version uint8, software Software) ([]byte, error) { if version < 4 { return []byte{uint8(b.redistDefault)}, nil } @@ -1836,7 +1840,7 @@ func (b *helloBody) serialize(version uint8, software Software) ([]byte, error) return buf, nil } -func (b *helloBody) string(version uint8, software Software) string { +func (b *HelloBody) string(version uint8, software Software) string { return fmt.Sprintf( "route_type: %s, instance :%d, sessionID: %d, receiveNotify: %d, synchronous: %d", b.redistDefault.String(), b.instance, b.sessionID, b.receiveNotify, b.synchronous) @@ -2160,7 +2164,7 @@ type Nexthop struct { LabelNum uint8 //FRR5, FRR6, FRR7.x, FRR8, FRR8.1 MplsLabels []uint32 //FRR5, FRR6, FRR7.x, FRR8, FRR8.1 rmac [6]byte //FRR6, FRR7.x, FRR8, FRR8.1 - weight uint32 //FRR7.3, FRR7.4, FRR7.5, FRR8, FRR8.1 + Weight uint32 //FRR7.3, FRR7.4, FRR7.5, FRR8, FRR8.1 backupNum uint8 //FRR7.4, FRR7.5, FRR8, FRR8.1 backupIndex []uint8 //FRR7.5, FRR8, FRR8.1 srteColor uint32 //FRR7.5, FRR8, FRR8.1 @@ -2174,7 +2178,7 @@ func (n Nexthop) string() string { s = append(s, fmt.Sprintf( "type: %s, vrf_id: %d, ifindex: %d, flags: %d, gate: %s, blackholeType: %d, label_num: %d, weight: %d, backupNum: %d, srteColor: %d", n.Type.String(), n.VrfID, n.Ifindex, n.flags, n.Gate.String(), - n.blackholeType, n.LabelNum, n.weight, n.backupNum, n.srteColor)) + n.blackholeType, n.LabelNum, n.Weight, n.backupNum, n.srteColor)) for i := uint8(0); i < n.LabelNum; i++ { s = append(s, fmt.Sprintf(" label[%d]: %d", i, n.MplsLabels[i])) } @@ -2220,7 +2224,7 @@ func (n Nexthop) encode(version uint8, software Software, processFlag nexthopPro if n.LabelNum > 0 { n.flags |= zapiNexthopFlagLabel } - if n.weight > 0 { + if n.Weight > 0 { n.flags |= zapiNexthopFlagWeight } if n.backupNum > 0 { @@ -2274,10 +2278,10 @@ func (n Nexthop) encode(version uint8, software Software, processFlag nexthopPro //frr: stream_put(s, &api_nh->labels[0], api_nh->label_num * sizeof(mpls_label_t)); buf = append(buf, tmpbuf...) } - if n.flags&zapiNexthopFlagWeight > 0 && n.weight > 0 { + if n.flags&zapiNexthopFlagWeight > 0 && n.Weight > 0 { tmpbuf := make([]byte, 4) - binary.BigEndian.PutUint32(tmpbuf, uint32(n.weight)) - buf = append(buf, tmpbuf...) //frr: stream_putl(s, api_nh->weight); + binary.BigEndian.PutUint32(tmpbuf, uint32(n.Weight)) + buf = append(buf, tmpbuf...) //frr: stream_putl(s, api_nh->Weight); } if apiFlag&flagEvpnRoute.ToEach(version, software) > 0 { //frr: stream_put(s, &(api_nh->rmac), sizeof(struct ethaddr)); @@ -2389,8 +2393,8 @@ func (n *Nexthop) decode(data []byte, version uint8, software Software, family u } } if n.flags&zapiNexthopFlagWeight > 0 { - //frr: STREAM_GETL(s, api_nh->weight); - n.weight = binary.BigEndian.Uint32(data[offset:]) + //frr: STREAM_GETL(s, api_nh->Weight); + n.Weight = binary.BigEndian.Uint32(data[offset:]) offset += 4 } if apiFlag&flagEvpnRoute.ToEach(version, software) > 0 { @@ -2555,9 +2559,9 @@ func (b *IPRouteBody) RouteFamily(logger log.Logger, version uint8, software Sof // IsWithdraw is referred in zclient func (b *IPRouteBody) IsWithdraw(version uint8, software Software) bool { - api := b.API.toCommon(version, software) + api := b.API.ToCommon(version, software) switch api { - case RouteDelete, redistributeRouteDel, BackwardIPv6RouteDelete: + case RouteDelete, RedistributeRouteDel, BackwardIPv6RouteDelete: return true } if version == 4 && b.API == zapi4RedistributeIPv6Del { @@ -3528,7 +3532,7 @@ type Message struct { Body Body } -func (m *Message) serialize(software Software) ([]byte, error) { +func (m *Message) Serialize(software Software) ([]byte, error) { var body []byte if m.Body != nil { var err error @@ -3552,7 +3556,7 @@ func parseMessage(hdr *Header, data []byte, software Software) (m *Message, err InterfaceBFDDestUpdate, ImportCheckUpdate, BFDDestReplay, InterfaceVRFUpdate, InterfaceLinkParams, PWStatusUpdate */ - command := m.Header.Command.toCommon(m.Header.Version, software) + command := m.Header.Command.ToCommon(m.Header.Version, software) switch command { case interfaceAdd, interfaceDelete, interfaceUp, interfaceDown: m.Body = &interfaceUpdateBody{} @@ -3562,7 +3566,7 @@ func parseMessage(hdr *Header, data []byte, software Software) (m *Message, err m.Body = &routerIDUpdateBody{} case nexthopUpdate: m.Body = &NexthopUpdateBody{} - case redistributeRouteAdd, redistributeRouteDel: // for frr + case RedistributeRouteAdd, RedistributeRouteDel: // for frr m.Body = &IPRouteBody{API: m.Header.Command} case labelManagerConnect: // Note: Synchronous message m.Body = &labelManagerConnectBody{} diff --git a/internal/pkg/zebra/zapi_bsd.go b/pkg/zebra/zapi_bsd.go similarity index 100% rename from internal/pkg/zebra/zapi_bsd.go rename to pkg/zebra/zapi_bsd.go diff --git a/internal/pkg/zebra/zapi_darwin.go b/pkg/zebra/zapi_darwin.go similarity index 100% rename from internal/pkg/zebra/zapi_darwin.go rename to pkg/zebra/zapi_darwin.go diff --git a/internal/pkg/zebra/zapi_linux.go b/pkg/zebra/zapi_linux.go similarity index 100% rename from internal/pkg/zebra/zapi_linux.go rename to pkg/zebra/zapi_linux.go diff --git a/internal/pkg/zebra/zapi_test.go b/pkg/zebra/zapi_test.go similarity index 99% rename from internal/pkg/zebra/zapi_test.go rename to pkg/zebra/zapi_test.go index ae82b1251..b5f14b702 100644 --- a/internal/pkg/zebra/zapi_test.go +++ b/pkg/zebra/zapi_test.go @@ -968,7 +968,7 @@ func Test_NexthopRegisterBody(t *testing.T) { for v := MinZapiVer; v <= MaxZapiVer; v++ { // Test decodeFromBytes() software := NewSoftware(v, "") - b := &NexthopRegisterBody{api: command[v].toCommon(v, software)} + b := &NexthopRegisterBody{api: command[v].ToCommon(v, software)} err := b.decodeFromBytes(bufIn, v, software) assert.Nil(err) @@ -1052,7 +1052,7 @@ func Test_NexthopUpdateBody(t *testing.T) { // Test decodeFromBytes() software := NewSoftware(v, "") - b := &NexthopUpdateBody{API: command[v].toCommon(v, software)} + b := &NexthopUpdateBody{API: command[v].ToCommon(v, software)} err := b.decodeFromBytes(bufIn, v, software) assert.Nil(err) diff --git a/internal/pkg/zebra/zapi_windows.go b/pkg/zebra/zapi_windows.go similarity index 100% rename from internal/pkg/zebra/zapi_windows.go rename to pkg/zebra/zapi_windows.go diff --git a/tools/tools.go b/tools/tools.go index 7a24f57ef..ca8621e62 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -13,6 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +//go:build tools // +build tools package tools