diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2018-08-26 22:04:12 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2018-08-28 15:36:50 +0900 |
commit | 97ad6b32ee61fd439c2edefbc371f748cdf82046 (patch) | |
tree | 7e06da9270ed2fc826939f462662a75b9e7b1714 /pkg | |
parent | 23ac6ebb861a725273c6b663dc484becef55087c (diff) |
api: change Family
fixes #1812
Avoid the cast and use strightforward data strcuture; two variables
for afi and safi instead of squashing two into one variable.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/server/grpc_server.go | 36 | ||||
-rw-r--r-- | pkg/server/server.go | 22 |
2 files changed, 37 insertions, 21 deletions
diff --git a/pkg/server/grpc_server.go b/pkg/server/grpc_server.go index 009c0fd2..f9fc6133 100644 --- a/pkg/server/grpc_server.go +++ b/pkg/server/grpc_server.go @@ -122,8 +122,10 @@ func extractFamilyFromConfigAfiSafi(c *config.AfiSafi) uint32 { } func NewAfiSafiConfigFromConfigStruct(c *config.AfiSafi) *api.AfiSafiConfig { + rf := extractFamilyFromConfigAfiSafi(c) + afi, safi := bgp.RouteFamilyToAfiSafi(bgp.RouteFamily(rf)) return &api.AfiSafiConfig{ - Family: extractFamilyFromConfigAfiSafi(c), + Family: &api.Family{Afi: api.Family_Afi(afi), Safi: api.Family_Safi(safi)}, Enabled: c.Config.Enabled, } } @@ -186,9 +188,9 @@ 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: uint32(c.State.Family), + Family: &api.Family{Afi: api.Family_Afi(afi), Safi: api.Family_Safi(safi)}, MaxPrefixes: c.PrefixLimit.Config.MaxPrefixes, ShutdownThresholdPct: uint32(c.PrefixLimit.Config.ShutdownThresholdPct), } @@ -459,7 +461,6 @@ func NewValidationFromTableStruct(v *table.Validation) *api.RPKIValidation { func toPathAPI(binNlri []byte, binPattrs [][]byte, anyNlri *any.Any, anyPattrs []*any.Any, path *table.Path, v *table.Validation) *api.Path { nlri := path.GetNlri() - family := uint32(path.GetRouteFamily()) vv := config.RPKI_VALIDATION_RESULT_TYPE_NONE.ToInt() if v != nil { vv = v.Status.ToInt() @@ -471,7 +472,7 @@ func toPathAPI(binNlri []byte, binPattrs [][]byte, anyNlri *any.Any, anyPattrs [ IsWithdraw: path.IsWithdraw, Validation: int32(vv), ValidationDetail: NewValidationFromTableStruct(v), - Family: family, + Family: &api.Family{Afi: api.Family_Afi(nlri.AFI()), Safi: api.Family_Safi(nlri.SAFI())}, Stale: path.IsStale(), IsFromExternal: path.IsFromExternal(), NoImplicitWithdraw: path.NoImplicitWithdraw(), @@ -543,7 +544,8 @@ func (s *Server) MonitorTable(arg *api.MonitorTableRequest, stream api.GobgpApi_ sendPath := func(pathList []*table.Path) error { for _, path := range pathList { - if path == nil || (arg.Family != 0 && bgp.RouteFamily(arg.Family) != path.GetRouteFamily()) { + f := bgp.AfiSafiToRouteFamily(uint16(arg.Family.Afi), uint8(arg.Family.Safi)) + if path == nil || (arg.Family != nil && f != path.GetRouteFamily()) { continue } if err := stream.Send(&api.MonitorTableResponse{Path: ToPathApi(path, nil)}); err != nil { @@ -732,8 +734,8 @@ func api2PathList(resource api.Resource, ApiPathList []*api.Path) ([]*table.Path } else if !path.IsWithdraw && nexthop == "" { return nil, fmt.Errorf("nexthop not found") } - - if resource != api.Resource_VRF && bgp.RouteFamily(path.Family) == bgp.RF_IPv4_UC && net.ParseIP(nexthop).To4() != nil { + rf := bgp.AfiSafiToRouteFamily(uint16(path.Family.Afi), uint8(path.Family.Safi)) + if resource != api.Resource_VRF && rf == bgp.RF_IPv4_UC && net.ParseIP(nexthop).To4() != nil { pattrs = append(pattrs, bgp.NewPathAttributeNextHop(nexthop)) } else { pattrs = append(pattrs, bgp.NewPathAttributeMpReachNLRI(nexthop, []bgp.AddrPrefixInterface{nlri})) @@ -885,7 +887,8 @@ func ReadAfiSafiConfigFromAPIStruct(c *config.AfiSafiConfig, a *api.AfiSafiConfi if c == nil || a == nil { return } - c.AfiSafiName = config.AfiSafiType(bgp.RouteFamily(a.Family).String()) + rf := bgp.AfiSafiToRouteFamily(uint16(a.Family.Afi), uint8(a.Family.Safi)) + c.AfiSafiName = config.AfiSafiType(rf.String()) c.Enabled = a.Enabled } @@ -894,7 +897,7 @@ func ReadAfiSafiStateFromAPIStruct(s *config.AfiSafiState, a *api.AfiSafiConfig) return } // Store only address family value for the convenience - s.Family = bgp.RouteFamily(a.Family) + s.Family = bgp.AfiSafiToRouteFamily(uint16(a.Family.Afi), uint8(a.Family.Safi)) } func ReadPrefixLimitFromAPIStruct(c *config.PrefixLimit, a *api.PrefixLimit) { @@ -1075,7 +1078,8 @@ func NewNeighborFromAPIStruct(a *api.Peer) (*config.Neighbor, error) { for _, afiSafi := range pconf.AfiSafis { // If Peer.Conf.PrefixLimits contains the configuration for // the same address family, we override AfiSafi.PrefixLimit. - if uint32(afiSafi.State.Family) == prefixLimit.Family { + rf := bgp.AfiSafiToRouteFamily(uint16(prefixLimit.Family.Afi), uint8(prefixLimit.Family.Safi)) + if uint32(afiSafi.State.Family) == uint32(rf) { ReadPrefixLimitFromAPIStruct(&afiSafi.PrefixLimit, prefixLimit) } } @@ -1656,10 +1660,11 @@ func toStatementApi(s *config.Statement) *api.Statement { cs.NextHopInList = s.Conditions.BgpConditions.NextHopInList } if s.Conditions.BgpConditions.AfiSafiInList != nil { - afiSafiIn := make([]api.Family, 0) + afiSafiIn := make([]*api.Family, 0) for _, afiSafiType := range s.Conditions.BgpConditions.AfiSafiInList { if mapped, ok := bgp.AddressFamilyValueMap[string(afiSafiType)]; ok { - afiSafiIn = append(afiSafiIn, api.Family(mapped)) + afi, safi := bgp.RouteFamilyToAfiSafi(mapped) + afiSafiIn = append(afiSafiIn, &api.Family{Afi: api.Family_Afi(afi), Safi: api.Family_Safi(safi)}) } } cs.AfiSafiIn = afiSafiIn @@ -1923,13 +1928,14 @@ func NewNextHopConditionFromApiStruct(a []string) (*table.NextHopCondition, erro return table.NewNextHopCondition(a) } -func NewAfiSafiInConditionFromApiStruct(a []api.Family) (*table.AfiSafiInCondition, error) { +func NewAfiSafiInConditionFromApiStruct(a []*api.Family) (*table.AfiSafiInCondition, error) { if a == nil { return nil, nil } afiSafiTypes := make([]config.AfiSafiType, 0, len(a)) for _, aType := range a { - if configType, ok := bgp.AddressFamilyNameMap[bgp.RouteFamily(aType)]; ok { + rf := bgp.AfiSafiToRouteFamily(uint16(aType.Afi), uint8(aType.Safi)) + if configType, ok := bgp.AddressFamilyNameMap[bgp.RouteFamily(rf)]; ok { afiSafiTypes = append(afiSafiTypes, config.AfiSafiType(configType)) } else { return nil, fmt.Errorf("unknown afi-safi-in type value: %d", aType) diff --git a/pkg/server/server.go b/pkg/server/server.go index c76ec45d..8f471d11 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -1859,7 +1859,6 @@ func (s *BgpServer) DeletePath(ctx context.Context, r *api.DeletePathRequest) er if err != nil { return err } - f := bgp.RouteFamily(r.Family) if len(r.Uuid) > 0 { // Delete locally generated path which has the given UUID @@ -1884,8 +1883,9 @@ func (s *BgpServer) DeletePath(ctx context.Context, r *api.DeletePathRequest) er } else if len(pathList) == 0 { // Delete all locally generated paths families := s.globalRib.GetRFlist() - if f != 0 { - families = []bgp.RouteFamily{f} + if r.Family != nil { + families = []bgp.RouteFamily{bgp.AfiSafiToRouteFamily(uint16(r.Family.Afi), uint8(r.Family.Safi))} + } for _, path := range s.globalRib.GetPathList(table.GLOBAL_RIB_NAME, 0, families) { if path.IsLocal() { @@ -2297,7 +2297,10 @@ func (s *BgpServer) ListPath(ctx context.Context, r *api.ListPathRequest) ([]*ap } in := false - family := bgp.RouteFamily(r.Family) + family := bgp.RouteFamily(0) + if r.Family != nil { + family = bgp.AfiSafiToRouteFamily(uint16(r.Family.Afi), uint8(r.Family.Safi)) + } var err error switch r.Type { case api.Resource_LOCAL, api.Resource_GLOBAL: @@ -2390,7 +2393,10 @@ func (s *BgpServer) GetTable(ctx context.Context, r *api.GetTableRequest) (*api. if r == nil || r.Name == "" { return nil, fmt.Errorf("invalid request") } - family := bgp.RouteFamily(r.Family) + family := bgp.RouteFamily(0) + if r.Family != nil { + family = bgp.AfiSafiToRouteFamily(uint16(r.Family.Afi), uint8(r.Family.Safi)) + } var in bool var err error var info *table.TableInfo @@ -3287,7 +3293,11 @@ func (s *BgpServer) ListRpki(ctx context.Context, r *api.ListRpkiRequest) (l []* func (s *BgpServer) ListRpkiTable(ctx context.Context, r *api.ListRpkiTableRequest) (l []*api.Roa, err error) { s.mgmtOperation(func() error { var roas []*table.ROA - roas, err = s.roaManager.GetRoa(bgp.RouteFamily(r.Family)) + family := bgp.RouteFamily(0) + if r.Family != nil { + family = bgp.AfiSafiToRouteFamily(uint16(r.Family.Afi), uint8(r.Family.Safi)) + } + roas, err = s.roaManager.GetRoa(family) if err == nil { l = append(l, NewRoaListFromTableStructList(roas)...) } |