diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2018-10-27 20:48:18 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2018-10-28 21:25:49 +0900 |
commit | d032c3153e72ab3c01355a20098ac1b68d26cba5 (patch) | |
tree | 5123a1b7a2d1af5a7d95476efae028df0b6da535 | |
parent | 23ae91cfc1a541b7033126c284e9a2e5814836ca (diff) |
move exported functions to generate api structs from config struct in server/
NewPeerFromConfigStruct()
NewPeerGroupFromConfigStruct()
NewGlobalFromConfigStruct()
The config is an internal package so they should not be exported.
We will remove the usage of the config in server/. This is a part of
the goal.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | cmd/gobgpd/main.go | 12 | ||||
-rw-r--r-- | internal/pkg/config/util.go | 380 | ||||
-rw-r--r-- | pkg/server/grpc_server.go | 377 | ||||
-rw-r--r-- | pkg/server/server.go | 2 | ||||
-rw-r--r-- | pkg/server/server_test.go | 10 |
5 files changed, 392 insertions, 389 deletions
diff --git a/cmd/gobgpd/main.go b/cmd/gobgpd/main.go index c212562c..4bff160f 100644 --- a/cmd/gobgpd/main.go +++ b/cmd/gobgpd/main.go @@ -185,7 +185,7 @@ func main() { if c == nil { c = newConfig if _, err := apiServer.StartBgp(context.Background(), &api.StartBgpRequest{ - Global: server.NewGlobalFromConfigStruct(&c.Global), + Global: config.NewGlobalFromConfigStruct(&c.Global), }); err != nil { log.Fatalf("failed to set global config: %s", err) } @@ -360,7 +360,7 @@ func main() { for _, pg := range addedPg { log.Infof("PeerGroup %s is added", pg.Config.PeerGroupName) if _, err := apiServer.AddPeerGroup(context.Background(), &api.AddPeerGroupRequest{ - PeerGroup: server.NewPeerGroupFromConfigStruct(&pg), + PeerGroup: config.NewPeerGroupFromConfigStruct(&pg), }); err != nil { log.Warn(err) } @@ -376,7 +376,7 @@ func main() { for _, pg := range updatedPg { log.Infof("PeerGroup %v is updated", pg.State.PeerGroupName) if u, err := apiServer.UpdatePeerGroup(context.Background(), &api.UpdatePeerGroupRequest{ - PeerGroup: server.NewPeerGroupFromConfigStruct(&pg), + PeerGroup: config.NewPeerGroupFromConfigStruct(&pg), }); err != nil { log.Warn(err) } else { @@ -386,7 +386,7 @@ func main() { for _, pg := range updatedPg { log.Infof("PeerGroup %s is updated", pg.Config.PeerGroupName) if _, err := apiServer.UpdatePeerGroup(context.Background(), &api.UpdatePeerGroupRequest{ - PeerGroup: server.NewPeerGroupFromConfigStruct(&pg), + PeerGroup: config.NewPeerGroupFromConfigStruct(&pg), }); err != nil { log.Warn(err) } @@ -405,7 +405,7 @@ func main() { for _, p := range added { log.Infof("Peer %v is added", p.State.NeighborAddress) if _, err := apiServer.AddPeer(context.Background(), &api.AddPeerRequest{ - Peer: server.NewPeerFromConfigStruct(&p), + Peer: config.NewPeerFromConfigStruct(&p), }); err != nil { log.Warn(err) } @@ -421,7 +421,7 @@ func main() { for _, p := range updated { log.Infof("Peer %v is updated", p.State.NeighborAddress) if u, err := apiServer.UpdatePeer(context.Background(), &api.UpdatePeerRequest{ - Peer: server.NewPeerFromConfigStruct(&p), + Peer: config.NewPeerFromConfigStruct(&p), }); err != nil { log.Warn(err) } else { diff --git a/internal/pkg/config/util.go b/internal/pkg/config/util.go index 591252b4..47c7d37f 100644 --- a/internal/pkg/config/util.go +++ b/internal/pkg/config/util.go @@ -21,7 +21,10 @@ import ( "path/filepath" "regexp" "strconv" + "strings" + api "github.com/osrg/gobgp/api" + "github.com/osrg/gobgp/internal/pkg/apiutil" "github.com/osrg/gobgp/pkg/packet/bgp" ) @@ -262,3 +265,380 @@ func ParseMaskLength(prefix, mask string) (int, int, error) { } return int(min), int(max), nil } + +func extractFamilyFromConfigAfiSafi(c *AfiSafi) uint32 { + if c == nil { + return 0 + } + // If address family value is already stored in AfiSafiState structure, + // we prefer to use this value. + if c.State.Family != 0 { + return uint32(c.State.Family) + } + // In case that Neighbor structure came from CLI or gRPC, address family + // value in AfiSafiState structure can be omitted. + // Here extracts value from AfiSafiName field in AfiSafiConfig structure. + if rf, err := bgp.GetRouteFamily(string(c.Config.AfiSafiName)); err == nil { + return uint32(rf) + } + // Ignores invalid address family name + return 0 +} + +func newAfiSafiConfigFromConfigStruct(c *AfiSafi) *api.AfiSafiConfig { + rf := extractFamilyFromConfigAfiSafi(c) + afi, safi := bgp.RouteFamilyToAfiSafi(bgp.RouteFamily(rf)) + return &api.AfiSafiConfig{ + Family: &api.Family{Afi: api.Family_Afi(afi), Safi: api.Family_Safi(safi)}, + Enabled: c.Config.Enabled, + } +} + +func newApplyPolicyFromConfigStruct(c *ApplyPolicy) *api.ApplyPolicy { + applyPolicy := &api.ApplyPolicy{ + ImportPolicy: &api.PolicyAssignment{ + Direction: api.PolicyDirection_IMPORT, + DefaultAction: api.RouteAction(c.Config.DefaultImportPolicy.ToInt()), + }, + ExportPolicy: &api.PolicyAssignment{ + Direction: api.PolicyDirection_EXPORT, + DefaultAction: api.RouteAction(c.Config.DefaultExportPolicy.ToInt()), + }, + } + + for _, pname := range c.Config.ImportPolicyList { + applyPolicy.ImportPolicy.Policies = append(applyPolicy.ImportPolicy.Policies, &api.Policy{Name: pname}) + } + for _, pname := range c.Config.ExportPolicyList { + applyPolicy.ExportPolicy.Policies = append(applyPolicy.ExportPolicy.Policies, &api.Policy{Name: pname}) + } + + return applyPolicy +} + +func newPrefixLimitFromConfigStruct(c *AfiSafi) *api.PrefixLimit { + if c.PrefixLimit.Config.MaxPrefixes == 0 { + return nil + } + afi, safi := bgp.RouteFamilyToAfiSafi(bgp.RouteFamily(c.State.Family)) + return &api.PrefixLimit{ + Family: &api.Family{Afi: api.Family_Afi(afi), Safi: api.Family_Safi(safi)}, + MaxPrefixes: c.PrefixLimit.Config.MaxPrefixes, + ShutdownThresholdPct: uint32(c.PrefixLimit.Config.ShutdownThresholdPct), + } +} + +func newRouteTargetMembershipFromConfigStruct(c *RouteTargetMembership) *api.RouteTargetMembership { + return &api.RouteTargetMembership{ + Config: &api.RouteTargetMembershipConfig{ + DeferralTime: uint32(c.Config.DeferralTime), + }, + } +} + +func newLongLivedGracefulRestartFromConfigStruct(c *LongLivedGracefulRestart) *api.LongLivedGracefulRestart { + return &api.LongLivedGracefulRestart{ + Config: &api.LongLivedGracefulRestartConfig{ + Enabled: c.Config.Enabled, + RestartTime: c.Config.RestartTime, + }, + } +} + +func newAddPathsFromConfigStruct(c *AddPaths) *api.AddPaths { + return &api.AddPaths{ + Config: &api.AddPathsConfig{ + Receive: c.Config.Receive, + SendMax: uint32(c.Config.SendMax), + }, + } +} + +func newRouteSelectionOptionsFromConfigStruct(c *RouteSelectionOptions) *api.RouteSelectionOptions { + return &api.RouteSelectionOptions{ + Config: &api.RouteSelectionOptionsConfig{ + AlwaysCompareMed: c.Config.AlwaysCompareMed, + IgnoreAsPathLength: c.Config.IgnoreAsPathLength, + ExternalCompareRouterId: c.Config.ExternalCompareRouterId, + AdvertiseInactiveRoutes: c.Config.AdvertiseInactiveRoutes, + EnableAigp: c.Config.EnableAigp, + IgnoreNextHopIgpMetric: c.Config.IgnoreNextHopIgpMetric, + }, + } +} + +func newMpGracefulRestartFromConfigStruct(c *MpGracefulRestart) *api.MpGracefulRestart { + return &api.MpGracefulRestart{ + Config: &api.MpGracefulRestartConfig{ + Enabled: c.Config.Enabled, + }, + } +} + +func newUseMultiplePathsFromConfigStruct(c *UseMultiplePaths) *api.UseMultiplePaths { + return &api.UseMultiplePaths{ + Config: &api.UseMultiplePathsConfig{ + Enabled: c.Config.Enabled, + }, + Ebgp: &api.Ebgp{ + Config: &api.EbgpConfig{ + AllowMultipleAs: c.Ebgp.Config.AllowMultipleAs, + MaximumPaths: c.Ebgp.Config.MaximumPaths, + }, + }, + Ibgp: &api.Ibgp{ + Config: &api.IbgpConfig{ + MaximumPaths: c.Ibgp.Config.MaximumPaths, + }, + }, + } +} + +func newAfiSafiFromConfigStruct(c *AfiSafi) *api.AfiSafi { + return &api.AfiSafi{ + MpGracefulRestart: newMpGracefulRestartFromConfigStruct(&c.MpGracefulRestart), + Config: newAfiSafiConfigFromConfigStruct(c), + ApplyPolicy: newApplyPolicyFromConfigStruct(&c.ApplyPolicy), + RouteSelectionOptions: newRouteSelectionOptionsFromConfigStruct(&c.RouteSelectionOptions), + UseMultiplePaths: newUseMultiplePathsFromConfigStruct(&c.UseMultiplePaths), + PrefixLimits: newPrefixLimitFromConfigStruct(c), + RouteTargetMembership: newRouteTargetMembershipFromConfigStruct(&c.RouteTargetMembership), + LongLivedGracefulRestart: newLongLivedGracefulRestartFromConfigStruct(&c.LongLivedGracefulRestart), + AddPaths: newAddPathsFromConfigStruct(&c.AddPaths), + } +} + +func NewPeerFromConfigStruct(pconf *Neighbor) *api.Peer { + afiSafis := make([]*api.AfiSafi, 0, len(pconf.AfiSafis)) + for _, f := range pconf.AfiSafis { + if afiSafi := newAfiSafiFromConfigStruct(&f); afiSafi != nil { + afiSafis = append(afiSafis, afiSafi) + } + } + + timer := pconf.Timers + s := pconf.State + localAddress := pconf.Transport.Config.LocalAddress + if pconf.Transport.State.LocalAddress != "" { + localAddress = pconf.Transport.State.LocalAddress + } + remoteCap, err := apiutil.MarshalCapabilities(pconf.State.RemoteCapabilityList) + if err != nil { + return nil + } + localCap, err := apiutil.MarshalCapabilities(pconf.State.LocalCapabilityList) + if err != nil { + return nil + } + var removePrivateAs api.PeerConf_RemovePrivateAs + switch pconf.Config.RemovePrivateAs { + case REMOVE_PRIVATE_AS_OPTION_ALL: + removePrivateAs = api.PeerConf_ALL + case REMOVE_PRIVATE_AS_OPTION_REPLACE: + removePrivateAs = api.PeerConf_REPLACE + } + return &api.Peer{ + ApplyPolicy: newApplyPolicyFromConfigStruct(&pconf.ApplyPolicy), + Conf: &api.PeerConf{ + NeighborAddress: pconf.Config.NeighborAddress, + Id: s.RemoteRouterId, + PeerAs: pconf.Config.PeerAs, + LocalAs: pconf.Config.LocalAs, + PeerType: uint32(pconf.Config.PeerType.ToInt()), + AuthPassword: pconf.Config.AuthPassword, + RouteFlapDamping: pconf.Config.RouteFlapDamping, + Description: pconf.Config.Description, + PeerGroup: pconf.Config.PeerGroup, + RemoteCap: remoteCap, + LocalCap: localCap, + NeighborInterface: pconf.Config.NeighborInterface, + Vrf: pconf.Config.Vrf, + AllowOwnAs: uint32(pconf.AsPathOptions.Config.AllowOwnAs), + RemovePrivateAs: removePrivateAs, + ReplacePeerAs: pconf.AsPathOptions.Config.ReplacePeerAs, + }, + State: &api.PeerState{ + SessionState: api.PeerState_SessionState(api.PeerState_SessionState_value[strings.ToUpper(string(s.SessionState))]), + AdminState: api.PeerState_AdminState(s.AdminState.ToInt()), + Messages: &api.Messages{ + Received: &api.Message{ + Notification: s.Messages.Received.Notification, + Update: s.Messages.Received.Update, + Open: s.Messages.Received.Open, + Keepalive: s.Messages.Received.Keepalive, + Refresh: s.Messages.Received.Refresh, + Discarded: s.Messages.Received.Discarded, + Total: s.Messages.Received.Total, + }, + Sent: &api.Message{ + Notification: s.Messages.Sent.Notification, + Update: s.Messages.Sent.Update, + Open: s.Messages.Sent.Open, + Keepalive: s.Messages.Sent.Keepalive, + Refresh: s.Messages.Sent.Refresh, + Discarded: s.Messages.Sent.Discarded, + Total: s.Messages.Sent.Total, + }, + }, + Received: s.AdjTable.Received, + Accepted: s.AdjTable.Accepted, + Advertised: s.AdjTable.Advertised, + PeerAs: s.PeerAs, + PeerType: uint32(s.PeerType.ToInt()), + NeighborAddress: pconf.State.NeighborAddress, + Queues: &api.Queues{}, + }, + EbgpMultihop: &api.EbgpMultihop{ + Enabled: pconf.EbgpMultihop.Config.Enabled, + MultihopTtl: uint32(pconf.EbgpMultihop.Config.MultihopTtl), + }, + Timers: &api.Timers{ + Config: &api.TimersConfig{ + ConnectRetry: uint64(timer.Config.ConnectRetry), + HoldTime: uint64(timer.Config.HoldTime), + KeepaliveInterval: uint64(timer.Config.KeepaliveInterval), + }, + State: &api.TimersState{ + KeepaliveInterval: uint64(timer.State.KeepaliveInterval), + NegotiatedHoldTime: uint64(timer.State.NegotiatedHoldTime), + Uptime: uint64(timer.State.Uptime), + Downtime: uint64(timer.State.Downtime), + }, + }, + RouteReflector: &api.RouteReflector{ + RouteReflectorClient: pconf.RouteReflector.Config.RouteReflectorClient, + RouteReflectorClusterId: string(pconf.RouteReflector.State.RouteReflectorClusterId), + }, + RouteServer: &api.RouteServer{ + RouteServerClient: pconf.RouteServer.Config.RouteServerClient, + }, + GracefulRestart: &api.GracefulRestart{ + Enabled: pconf.GracefulRestart.Config.Enabled, + RestartTime: uint32(pconf.GracefulRestart.Config.RestartTime), + HelperOnly: pconf.GracefulRestart.Config.HelperOnly, + DeferralTime: uint32(pconf.GracefulRestart.Config.DeferralTime), + NotificationEnabled: pconf.GracefulRestart.Config.NotificationEnabled, + LonglivedEnabled: pconf.GracefulRestart.Config.LongLivedEnabled, + LocalRestarting: pconf.GracefulRestart.State.LocalRestarting, + }, + Transport: &api.Transport{ + RemotePort: uint32(pconf.Transport.Config.RemotePort), + LocalAddress: localAddress, + PassiveMode: pconf.Transport.Config.PassiveMode, + }, + AfiSafis: afiSafis, + AddPaths: newAddPathsFromConfigStruct(&pconf.AddPaths), + } +} + +func NewPeerGroupFromConfigStruct(pconf *PeerGroup) *api.PeerGroup { + afiSafis := make([]*api.AfiSafi, 0, len(pconf.AfiSafis)) + for _, f := range pconf.AfiSafis { + if afiSafi := newAfiSafiFromConfigStruct(&f); afiSafi != nil { + afiSafis = append(afiSafis, afiSafi) + } + } + + timer := pconf.Timers + s := pconf.State + return &api.PeerGroup{ + ApplyPolicy: newApplyPolicyFromConfigStruct(&pconf.ApplyPolicy), + Conf: &api.PeerGroupConf{ + PeerAs: pconf.Config.PeerAs, + LocalAs: pconf.Config.LocalAs, + PeerType: uint32(pconf.Config.PeerType.ToInt()), + AuthPassword: pconf.Config.AuthPassword, + RouteFlapDamping: pconf.Config.RouteFlapDamping, + Description: pconf.Config.Description, + PeerGroupName: pconf.Config.PeerGroupName, + }, + Info: &api.PeerGroupState{ + PeerAs: s.PeerAs, + PeerType: uint32(s.PeerType.ToInt()), + TotalPaths: s.TotalPaths, + TotalPrefixes: s.TotalPrefixes, + }, + Timers: &api.Timers{ + Config: &api.TimersConfig{ + ConnectRetry: uint64(timer.Config.ConnectRetry), + HoldTime: uint64(timer.Config.HoldTime), + KeepaliveInterval: uint64(timer.Config.KeepaliveInterval), + }, + State: &api.TimersState{ + KeepaliveInterval: uint64(timer.State.KeepaliveInterval), + NegotiatedHoldTime: uint64(timer.State.NegotiatedHoldTime), + Uptime: uint64(timer.State.Uptime), + Downtime: uint64(timer.State.Downtime), + }, + }, + RouteReflector: &api.RouteReflector{ + RouteReflectorClient: pconf.RouteReflector.Config.RouteReflectorClient, + RouteReflectorClusterId: string(pconf.RouteReflector.Config.RouteReflectorClusterId), + }, + RouteServer: &api.RouteServer{ + RouteServerClient: pconf.RouteServer.Config.RouteServerClient, + }, + GracefulRestart: &api.GracefulRestart{ + Enabled: pconf.GracefulRestart.Config.Enabled, + RestartTime: uint32(pconf.GracefulRestart.Config.RestartTime), + HelperOnly: pconf.GracefulRestart.Config.HelperOnly, + DeferralTime: uint32(pconf.GracefulRestart.Config.DeferralTime), + NotificationEnabled: pconf.GracefulRestart.Config.NotificationEnabled, + LonglivedEnabled: pconf.GracefulRestart.Config.LongLivedEnabled, + LocalRestarting: pconf.GracefulRestart.State.LocalRestarting, + }, + Transport: &api.Transport{ + RemotePort: uint32(pconf.Transport.Config.RemotePort), + LocalAddress: pconf.Transport.Config.LocalAddress, + PassiveMode: pconf.Transport.Config.PassiveMode, + }, + AfiSafis: afiSafis, + AddPaths: newAddPathsFromConfigStruct(&pconf.AddPaths), + } +} + +func NewGlobalFromConfigStruct(c *Global) *api.Global { + families := make([]uint32, 0, len(c.AfiSafis)) + for _, f := range c.AfiSafis { + families = append(families, uint32(AfiSafiTypeToIntMap[f.Config.AfiSafiName])) + } + + applyPolicy := newApplyPolicyFromConfigStruct(&c.ApplyPolicy) + + return &api.Global{ + As: c.Config.As, + RouterId: c.Config.RouterId, + ListenPort: c.Config.Port, + ListenAddresses: c.Config.LocalAddressList, + Families: families, + UseMultiplePaths: c.UseMultiplePaths.Config.Enabled, + RouteSelectionOptions: &api.RouteSelectionOptionsConfig{ + AlwaysCompareMed: c.RouteSelectionOptions.Config.AlwaysCompareMed, + IgnoreAsPathLength: c.RouteSelectionOptions.Config.IgnoreAsPathLength, + ExternalCompareRouterId: c.RouteSelectionOptions.Config.ExternalCompareRouterId, + AdvertiseInactiveRoutes: c.RouteSelectionOptions.Config.AdvertiseInactiveRoutes, + EnableAigp: c.RouteSelectionOptions.Config.EnableAigp, + IgnoreNextHopIgpMetric: c.RouteSelectionOptions.Config.IgnoreNextHopIgpMetric, + DisableBestPathSelection: c.RouteSelectionOptions.Config.DisableBestPathSelection, + }, + DefaultRouteDistance: &api.DefaultRouteDistance{ + ExternalRouteDistance: uint32(c.DefaultRouteDistance.Config.ExternalRouteDistance), + InternalRouteDistance: uint32(c.DefaultRouteDistance.Config.InternalRouteDistance), + }, + Confederation: &api.Confederation{ + Enabled: c.Confederation.Config.Enabled, + Identifier: c.Confederation.Config.Identifier, + MemberAsList: c.Confederation.Config.MemberAsList, + }, + GracefulRestart: &api.GracefulRestart{ + Enabled: c.GracefulRestart.Config.Enabled, + RestartTime: uint32(c.GracefulRestart.Config.RestartTime), + StaleRoutesTime: uint32(c.GracefulRestart.Config.StaleRoutesTime), + HelperOnly: c.GracefulRestart.Config.HelperOnly, + DeferralTime: uint32(c.GracefulRestart.Config.DeferralTime), + NotificationEnabled: c.GracefulRestart.Config.NotificationEnabled, + LonglivedEnabled: c.GracefulRestart.Config.LongLivedEnabled, + }, + ApplyPolicy: applyPolicy, + } +} diff --git a/pkg/server/grpc_server.go b/pkg/server/grpc_server.go index 4c2eb700..d5266310 100644 --- a/pkg/server/grpc_server.go +++ b/pkg/server/grpc_server.go @@ -94,337 +94,6 @@ func (s *Server) Serve() error { return nil } -func NewMpGracefulRestartFromConfigStruct(c *config.MpGracefulRestart) *api.MpGracefulRestart { - return &api.MpGracefulRestart{ - Config: &api.MpGracefulRestartConfig{ - Enabled: c.Config.Enabled, - }, - } -} - -func extractFamilyFromConfigAfiSafi(c *config.AfiSafi) uint32 { - if c == nil { - return 0 - } - // If address family value is already stored in AfiSafiState structure, - // we prefer to use this value. - if c.State.Family != 0 { - return uint32(c.State.Family) - } - // In case that Neighbor structure came from CLI or gRPC, address family - // value in AfiSafiState structure can be omitted. - // Here extracts value from AfiSafiName field in AfiSafiConfig structure. - if rf, err := bgp.GetRouteFamily(string(c.Config.AfiSafiName)); err == nil { - return uint32(rf) - } - // Ignores invalid address family name - return 0 -} - -func NewAfiSafiConfigFromConfigStruct(c *config.AfiSafi) *api.AfiSafiConfig { - rf := extractFamilyFromConfigAfiSafi(c) - afi, safi := bgp.RouteFamilyToAfiSafi(bgp.RouteFamily(rf)) - return &api.AfiSafiConfig{ - Family: &api.Family{Afi: api.Family_Afi(afi), Safi: api.Family_Safi(safi)}, - Enabled: c.Config.Enabled, - } -} - -func NewApplyPolicyFromConfigStruct(c *config.ApplyPolicy) *api.ApplyPolicy { - applyPolicy := &api.ApplyPolicy{ - ImportPolicy: &api.PolicyAssignment{ - Direction: api.PolicyDirection_IMPORT, - DefaultAction: api.RouteAction(c.Config.DefaultImportPolicy.ToInt()), - }, - ExportPolicy: &api.PolicyAssignment{ - Direction: api.PolicyDirection_EXPORT, - DefaultAction: api.RouteAction(c.Config.DefaultExportPolicy.ToInt()), - }, - } - - for _, pname := range c.Config.ImportPolicyList { - applyPolicy.ImportPolicy.Policies = append(applyPolicy.ImportPolicy.Policies, &api.Policy{Name: pname}) - } - for _, pname := range c.Config.ExportPolicyList { - applyPolicy.ExportPolicy.Policies = append(applyPolicy.ExportPolicy.Policies, &api.Policy{Name: pname}) - } - - return applyPolicy -} - -func NewRouteSelectionOptionsFromConfigStruct(c *config.RouteSelectionOptions) *api.RouteSelectionOptions { - return &api.RouteSelectionOptions{ - Config: &api.RouteSelectionOptionsConfig{ - AlwaysCompareMed: c.Config.AlwaysCompareMed, - IgnoreAsPathLength: c.Config.IgnoreAsPathLength, - ExternalCompareRouterId: c.Config.ExternalCompareRouterId, - AdvertiseInactiveRoutes: c.Config.AdvertiseInactiveRoutes, - EnableAigp: c.Config.EnableAigp, - IgnoreNextHopIgpMetric: c.Config.IgnoreNextHopIgpMetric, - }, - } -} - -func NewUseMultiplePathsFromConfigStruct(c *config.UseMultiplePaths) *api.UseMultiplePaths { - return &api.UseMultiplePaths{ - Config: &api.UseMultiplePathsConfig{ - Enabled: c.Config.Enabled, - }, - Ebgp: &api.Ebgp{ - Config: &api.EbgpConfig{ - AllowMultipleAs: c.Ebgp.Config.AllowMultipleAs, - MaximumPaths: c.Ebgp.Config.MaximumPaths, - }, - }, - Ibgp: &api.Ibgp{ - Config: &api.IbgpConfig{ - MaximumPaths: c.Ibgp.Config.MaximumPaths, - }, - }, - } -} - -func NewPrefixLimitFromConfigStruct(c *config.AfiSafi) *api.PrefixLimit { - if c.PrefixLimit.Config.MaxPrefixes == 0 { - return nil - } - afi, safi := bgp.RouteFamilyToAfiSafi(bgp.RouteFamily(c.State.Family)) - return &api.PrefixLimit{ - Family: &api.Family{Afi: api.Family_Afi(afi), Safi: api.Family_Safi(safi)}, - MaxPrefixes: c.PrefixLimit.Config.MaxPrefixes, - ShutdownThresholdPct: uint32(c.PrefixLimit.Config.ShutdownThresholdPct), - } -} - -func NewRouteTargetMembershipFromConfigStruct(c *config.RouteTargetMembership) *api.RouteTargetMembership { - return &api.RouteTargetMembership{ - Config: &api.RouteTargetMembershipConfig{ - DeferralTime: uint32(c.Config.DeferralTime), - }, - } -} - -func NewLongLivedGracefulRestartFromConfigStruct(c *config.LongLivedGracefulRestart) *api.LongLivedGracefulRestart { - return &api.LongLivedGracefulRestart{ - Config: &api.LongLivedGracefulRestartConfig{ - Enabled: c.Config.Enabled, - RestartTime: c.Config.RestartTime, - }, - } -} - -func NewAddPathsFromConfigStruct(c *config.AddPaths) *api.AddPaths { - return &api.AddPaths{ - Config: &api.AddPathsConfig{ - Receive: c.Config.Receive, - SendMax: uint32(c.Config.SendMax), - }, - } -} - -func NewAfiSafiFromConfigStruct(c *config.AfiSafi) *api.AfiSafi { - return &api.AfiSafi{ - MpGracefulRestart: NewMpGracefulRestartFromConfigStruct(&c.MpGracefulRestart), - Config: NewAfiSafiConfigFromConfigStruct(c), - ApplyPolicy: NewApplyPolicyFromConfigStruct(&c.ApplyPolicy), - RouteSelectionOptions: NewRouteSelectionOptionsFromConfigStruct(&c.RouteSelectionOptions), - UseMultiplePaths: NewUseMultiplePathsFromConfigStruct(&c.UseMultiplePaths), - PrefixLimits: NewPrefixLimitFromConfigStruct(c), - RouteTargetMembership: NewRouteTargetMembershipFromConfigStruct(&c.RouteTargetMembership), - LongLivedGracefulRestart: NewLongLivedGracefulRestartFromConfigStruct(&c.LongLivedGracefulRestart), - AddPaths: NewAddPathsFromConfigStruct(&c.AddPaths), - } -} - -func NewPeerFromConfigStruct(pconf *config.Neighbor) *api.Peer { - afiSafis := make([]*api.AfiSafi, 0, len(pconf.AfiSafis)) - for _, f := range pconf.AfiSafis { - if afiSafi := NewAfiSafiFromConfigStruct(&f); afiSafi != nil { - afiSafis = append(afiSafis, afiSafi) - } - } - - timer := pconf.Timers - s := pconf.State - localAddress := pconf.Transport.Config.LocalAddress - if pconf.Transport.State.LocalAddress != "" { - localAddress = pconf.Transport.State.LocalAddress - } - remoteCap, err := apiutil.MarshalCapabilities(pconf.State.RemoteCapabilityList) - if err != nil { - return nil - } - localCap, err := apiutil.MarshalCapabilities(pconf.State.LocalCapabilityList) - if err != nil { - return nil - } - var removePrivateAs api.PeerConf_RemovePrivateAs - switch pconf.Config.RemovePrivateAs { - case config.REMOVE_PRIVATE_AS_OPTION_ALL: - removePrivateAs = api.PeerConf_ALL - case config.REMOVE_PRIVATE_AS_OPTION_REPLACE: - removePrivateAs = api.PeerConf_REPLACE - } - return &api.Peer{ - ApplyPolicy: NewApplyPolicyFromConfigStruct(&pconf.ApplyPolicy), - Conf: &api.PeerConf{ - NeighborAddress: pconf.Config.NeighborAddress, - Id: s.RemoteRouterId, - PeerAs: pconf.Config.PeerAs, - LocalAs: pconf.Config.LocalAs, - PeerType: uint32(pconf.Config.PeerType.ToInt()), - AuthPassword: pconf.Config.AuthPassword, - RouteFlapDamping: pconf.Config.RouteFlapDamping, - Description: pconf.Config.Description, - PeerGroup: pconf.Config.PeerGroup, - RemoteCap: remoteCap, - LocalCap: localCap, - NeighborInterface: pconf.Config.NeighborInterface, - Vrf: pconf.Config.Vrf, - AllowOwnAs: uint32(pconf.AsPathOptions.Config.AllowOwnAs), - RemovePrivateAs: removePrivateAs, - ReplacePeerAs: pconf.AsPathOptions.Config.ReplacePeerAs, - }, - State: &api.PeerState{ - SessionState: api.PeerState_SessionState(api.PeerState_SessionState_value[strings.ToUpper(string(s.SessionState))]), - AdminState: api.PeerState_AdminState(s.AdminState.ToInt()), - Messages: &api.Messages{ - Received: &api.Message{ - Notification: s.Messages.Received.Notification, - Update: s.Messages.Received.Update, - Open: s.Messages.Received.Open, - Keepalive: s.Messages.Received.Keepalive, - Refresh: s.Messages.Received.Refresh, - Discarded: s.Messages.Received.Discarded, - Total: s.Messages.Received.Total, - }, - Sent: &api.Message{ - Notification: s.Messages.Sent.Notification, - Update: s.Messages.Sent.Update, - Open: s.Messages.Sent.Open, - Keepalive: s.Messages.Sent.Keepalive, - Refresh: s.Messages.Sent.Refresh, - Discarded: s.Messages.Sent.Discarded, - Total: s.Messages.Sent.Total, - }, - }, - Received: s.AdjTable.Received, - Accepted: s.AdjTable.Accepted, - Advertised: s.AdjTable.Advertised, - PeerAs: s.PeerAs, - PeerType: uint32(s.PeerType.ToInt()), - NeighborAddress: pconf.State.NeighborAddress, - Queues: &api.Queues{}, - }, - EbgpMultihop: &api.EbgpMultihop{ - Enabled: pconf.EbgpMultihop.Config.Enabled, - MultihopTtl: uint32(pconf.EbgpMultihop.Config.MultihopTtl), - }, - Timers: &api.Timers{ - Config: &api.TimersConfig{ - ConnectRetry: uint64(timer.Config.ConnectRetry), - HoldTime: uint64(timer.Config.HoldTime), - KeepaliveInterval: uint64(timer.Config.KeepaliveInterval), - }, - State: &api.TimersState{ - KeepaliveInterval: uint64(timer.State.KeepaliveInterval), - NegotiatedHoldTime: uint64(timer.State.NegotiatedHoldTime), - Uptime: uint64(timer.State.Uptime), - Downtime: uint64(timer.State.Downtime), - }, - }, - RouteReflector: &api.RouteReflector{ - RouteReflectorClient: pconf.RouteReflector.Config.RouteReflectorClient, - RouteReflectorClusterId: string(pconf.RouteReflector.State.RouteReflectorClusterId), - }, - RouteServer: &api.RouteServer{ - RouteServerClient: pconf.RouteServer.Config.RouteServerClient, - }, - GracefulRestart: &api.GracefulRestart{ - Enabled: pconf.GracefulRestart.Config.Enabled, - RestartTime: uint32(pconf.GracefulRestart.Config.RestartTime), - HelperOnly: pconf.GracefulRestart.Config.HelperOnly, - DeferralTime: uint32(pconf.GracefulRestart.Config.DeferralTime), - NotificationEnabled: pconf.GracefulRestart.Config.NotificationEnabled, - LonglivedEnabled: pconf.GracefulRestart.Config.LongLivedEnabled, - LocalRestarting: pconf.GracefulRestart.State.LocalRestarting, - }, - Transport: &api.Transport{ - RemotePort: uint32(pconf.Transport.Config.RemotePort), - LocalAddress: localAddress, - PassiveMode: pconf.Transport.Config.PassiveMode, - }, - AfiSafis: afiSafis, - AddPaths: NewAddPathsFromConfigStruct(&pconf.AddPaths), - } -} - -func NewPeerGroupFromConfigStruct(pconf *config.PeerGroup) *api.PeerGroup { - afiSafis := make([]*api.AfiSafi, 0, len(pconf.AfiSafis)) - for _, f := range pconf.AfiSafis { - if afiSafi := NewAfiSafiFromConfigStruct(&f); afiSafi != nil { - afiSafis = append(afiSafis, afiSafi) - } - } - - timer := pconf.Timers - s := pconf.State - return &api.PeerGroup{ - ApplyPolicy: NewApplyPolicyFromConfigStruct(&pconf.ApplyPolicy), - Conf: &api.PeerGroupConf{ - PeerAs: pconf.Config.PeerAs, - LocalAs: pconf.Config.LocalAs, - PeerType: uint32(pconf.Config.PeerType.ToInt()), - AuthPassword: pconf.Config.AuthPassword, - RouteFlapDamping: pconf.Config.RouteFlapDamping, - Description: pconf.Config.Description, - PeerGroupName: pconf.Config.PeerGroupName, - }, - Info: &api.PeerGroupState{ - PeerAs: s.PeerAs, - PeerType: uint32(s.PeerType.ToInt()), - TotalPaths: s.TotalPaths, - TotalPrefixes: s.TotalPrefixes, - }, - Timers: &api.Timers{ - Config: &api.TimersConfig{ - ConnectRetry: uint64(timer.Config.ConnectRetry), - HoldTime: uint64(timer.Config.HoldTime), - KeepaliveInterval: uint64(timer.Config.KeepaliveInterval), - }, - State: &api.TimersState{ - KeepaliveInterval: uint64(timer.State.KeepaliveInterval), - NegotiatedHoldTime: uint64(timer.State.NegotiatedHoldTime), - Uptime: uint64(timer.State.Uptime), - Downtime: uint64(timer.State.Downtime), - }, - }, - RouteReflector: &api.RouteReflector{ - RouteReflectorClient: pconf.RouteReflector.Config.RouteReflectorClient, - RouteReflectorClusterId: string(pconf.RouteReflector.Config.RouteReflectorClusterId), - }, - RouteServer: &api.RouteServer{ - RouteServerClient: pconf.RouteServer.Config.RouteServerClient, - }, - GracefulRestart: &api.GracefulRestart{ - Enabled: pconf.GracefulRestart.Config.Enabled, - RestartTime: uint32(pconf.GracefulRestart.Config.RestartTime), - HelperOnly: pconf.GracefulRestart.Config.HelperOnly, - DeferralTime: uint32(pconf.GracefulRestart.Config.DeferralTime), - NotificationEnabled: pconf.GracefulRestart.Config.NotificationEnabled, - LonglivedEnabled: pconf.GracefulRestart.Config.LongLivedEnabled, - LocalRestarting: pconf.GracefulRestart.State.LocalRestarting, - }, - Transport: &api.Transport{ - RemotePort: uint32(pconf.Transport.Config.RemotePort), - LocalAddress: pconf.Transport.Config.LocalAddress, - PassiveMode: pconf.Transport.Config.PassiveMode, - }, - AfiSafis: afiSafis, - AddPaths: NewAddPathsFromConfigStruct(&pconf.AddPaths), - } -} - func (s *Server) ListPeer(r *api.ListPeerRequest, stream api.GobgpApi_ListPeerServer) error { l, err := s.bgpServer.ListPeer(context.Background(), r) for _, e := range l { @@ -2342,52 +2011,6 @@ func NewGlobalFromAPIStruct(a *api.Global) *config.Global { return global } -func NewGlobalFromConfigStruct(c *config.Global) *api.Global { - families := make([]uint32, 0, len(c.AfiSafis)) - for _, f := range c.AfiSafis { - families = append(families, uint32(config.AfiSafiTypeToIntMap[f.Config.AfiSafiName])) - } - - applyPolicy := NewApplyPolicyFromConfigStruct(&c.ApplyPolicy) - - return &api.Global{ - As: c.Config.As, - RouterId: c.Config.RouterId, - ListenPort: c.Config.Port, - ListenAddresses: c.Config.LocalAddressList, - Families: families, - UseMultiplePaths: c.UseMultiplePaths.Config.Enabled, - RouteSelectionOptions: &api.RouteSelectionOptionsConfig{ - AlwaysCompareMed: c.RouteSelectionOptions.Config.AlwaysCompareMed, - IgnoreAsPathLength: c.RouteSelectionOptions.Config.IgnoreAsPathLength, - ExternalCompareRouterId: c.RouteSelectionOptions.Config.ExternalCompareRouterId, - AdvertiseInactiveRoutes: c.RouteSelectionOptions.Config.AdvertiseInactiveRoutes, - EnableAigp: c.RouteSelectionOptions.Config.EnableAigp, - IgnoreNextHopIgpMetric: c.RouteSelectionOptions.Config.IgnoreNextHopIgpMetric, - DisableBestPathSelection: c.RouteSelectionOptions.Config.DisableBestPathSelection, - }, - DefaultRouteDistance: &api.DefaultRouteDistance{ - ExternalRouteDistance: uint32(c.DefaultRouteDistance.Config.ExternalRouteDistance), - InternalRouteDistance: uint32(c.DefaultRouteDistance.Config.InternalRouteDistance), - }, - Confederation: &api.Confederation{ - Enabled: c.Confederation.Config.Enabled, - Identifier: c.Confederation.Config.Identifier, - MemberAsList: c.Confederation.Config.MemberAsList, - }, - GracefulRestart: &api.GracefulRestart{ - Enabled: c.GracefulRestart.Config.Enabled, - RestartTime: uint32(c.GracefulRestart.Config.RestartTime), - StaleRoutesTime: uint32(c.GracefulRestart.Config.StaleRoutesTime), - HelperOnly: c.GracefulRestart.Config.HelperOnly, - DeferralTime: uint32(c.GracefulRestart.Config.DeferralTime), - NotificationEnabled: c.GracefulRestart.Config.NotificationEnabled, - LonglivedEnabled: c.GracefulRestart.Config.LongLivedEnabled, - }, - ApplyPolicy: applyPolicy, - } -} - func (s *Server) StartBgp(ctx context.Context, r *api.StartBgpRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.StartBgp(ctx, r) } diff --git a/pkg/server/server.go b/pkg/server/server.go index 4aba3b50..a870429d 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -2477,7 +2477,7 @@ func (s *BgpServer) ListPeer(ctx context.Context, r *api.ListPeerRequest) ([]*ap continue } // FIXME: should remove toConfig() conversion - l = append(l, NewPeerFromConfigStruct(s.toConfig(peer, getAdvertised))) + l = append(l, config.NewPeerFromConfigStruct(s.toConfig(peer, getAdvertised))) } return nil }, false) diff --git a/pkg/server/server_test.go b/pkg/server/server_test.go index f1809e26..079f452c 100644 --- a/pkg/server/server_test.go +++ b/pkg/server/server_test.go @@ -218,7 +218,7 @@ func TestMonitor(test *testing.T) { }, }, } - err = t.AddPeer(context.Background(), &api.AddPeerRequest{Peer: NewPeerFromConfigStruct(m)}) + err = t.AddPeer(context.Background(), &api.AddPeerRequest{Peer: config.NewPeerFromConfigStruct(m)}) assert.Nil(err) for { @@ -566,7 +566,7 @@ func TestPeerGroup(test *testing.T) { }, } config.RegisterConfiguredFields("127.0.0.1", configured) - err = s.AddPeer(context.Background(), &api.AddPeerRequest{Peer: NewPeerFromConfigStruct(n)}) + err = s.AddPeer(context.Background(), &api.AddPeerRequest{Peer: config.NewPeerFromConfigStruct(n)}) assert.Nil(err) t := NewBgpServer() @@ -597,7 +597,7 @@ func TestPeerGroup(test *testing.T) { }, }, } - err = t.AddPeer(context.Background(), &api.AddPeerRequest{Peer: NewPeerFromConfigStruct(m)}) + err = t.AddPeer(context.Background(), &api.AddPeerRequest{Peer: config.NewPeerFromConfigStruct(m)}) assert.Nil(err) for { @@ -669,7 +669,7 @@ func TestDynamicNeighbor(t *testing.T) { }, }, } - err = s2.AddPeer(context.Background(), &api.AddPeerRequest{Peer: NewPeerFromConfigStruct(m)}) + err = s2.AddPeer(context.Background(), &api.AddPeerRequest{Peer: config.NewPeerFromConfigStruct(m)}) assert.Nil(err) @@ -886,7 +886,7 @@ func peerServers(t *testing.T, ctx context.Context, servers []*BgpServer, famili }) } - if err := server.AddPeer(ctx, &api.AddPeerRequest{Peer: NewPeerFromConfigStruct(neighborConfig)}); err != nil { + if err := server.AddPeer(ctx, &api.AddPeerRequest{Peer: config.NewPeerFromConfigStruct(neighborConfig)}); err != nil { t.Fatal(err) } } |