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 /cmd | |
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 'cmd')
-rw-r--r-- | cmd/gobgp/cmd/common.go | 121 | ||||
-rw-r--r-- | cmd/gobgp/cmd/global.go | 8 | ||||
-rw-r--r-- | cmd/gobgp/cmd/monitor.go | 8 | ||||
-rw-r--r-- | cmd/gobgp/cmd/neighbor.go | 28 | ||||
-rw-r--r-- | cmd/gobgp/cmd/policy.go | 7 | ||||
-rw-r--r-- | cmd/gobgp/cmd/rpki.go | 8 |
6 files changed, 130 insertions, 50 deletions
diff --git a/cmd/gobgp/cmd/common.go b/cmd/gobgp/cmd/common.go index f4a72b1e..855b4269 100644 --- a/cmd/gobgp/cmd/common.go +++ b/cmd/gobgp/cmd/common.go @@ -212,57 +212,130 @@ func newClient(ctx context.Context) (api.GobgpApiClient, error) { return api.NewGobgpApiClient(conn), nil } -func addr2AddressFamily(a net.IP) bgp.RouteFamily { +func addr2AddressFamily(a net.IP) *api.Family { if a.To4() != nil { - return bgp.RF_IPv4_UC + return &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_UNICAST, + } } else if a.To16() != nil { - return bgp.RF_IPv6_UC + return &api.Family{ + Afi: api.Family_AFI_IP6, + Safi: api.Family_SAFI_UNICAST, + } } - return bgp.RouteFamily(0) + return nil } -func checkAddressFamily(def bgp.RouteFamily) (bgp.RouteFamily, error) { - var rf bgp.RouteFamily +var ( + IPv4_UC = &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_UNICAST, + } + IPv6_UC = &api.Family{ + Afi: api.Family_AFI_IP6, + Safi: api.Family_SAFI_UNICAST, + } + IPv4_VPN = &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_MPLS_VPN, + } + IPv6_VPN = &api.Family{ + Afi: api.Family_AFI_IP6, + Safi: api.Family_SAFI_MPLS_VPN, + } + IPv4_MPLS = &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_MPLS_LABEL, + } + IPv6_MPLS = &api.Family{ + Afi: api.Family_AFI_IP6, + Safi: api.Family_SAFI_MPLS_LABEL, + } + EVPN = &api.Family{ + Afi: api.Family_AFI_L2VPN, + Safi: api.Family_SAFI_EVPN, + } + IPv4_ENCAP = &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_ENCAPSULATION, + } + IPv6_ENCAP = &api.Family{ + Afi: api.Family_AFI_IP6, + Safi: api.Family_SAFI_ENCAPSULATION, + } + RTC = &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_ROUTE_TARGET_CONSTRAINTS, + } + IPv4_FS = &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_FLOW_SPEC_UNICAST, + } + IPv6_FS = &api.Family{ + Afi: api.Family_AFI_IP6, + Safi: api.Family_SAFI_FLOW_SPEC_UNICAST, + } + IPv4_VPN_FS = &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_FLOW_SPEC_VPN, + } + IPv6_VPN_FS = &api.Family{ + Afi: api.Family_AFI_IP6, + Safi: api.Family_SAFI_FLOW_SPEC_VPN, + } + L2_VPN_FS = &api.Family{ + Afi: api.Family_AFI_L2VPN, + Safi: api.Family_SAFI_FLOW_SPEC_VPN, + } + OPAQUE = &api.Family{ + Afi: api.Family_AFI_OPAQUE, + Safi: api.Family_SAFI_KEY_VALUE, + } +) + +func checkAddressFamily(def *api.Family) (*api.Family, error) { + var f *api.Family var e error switch subOpts.AddressFamily { case "ipv4", "v4", "4": - rf = bgp.RF_IPv4_UC + f = IPv4_UC case "ipv6", "v6", "6": - rf = bgp.RF_IPv6_UC + f = IPv6_UC case "ipv4-l3vpn", "vpnv4", "vpn-ipv4": - rf = bgp.RF_IPv4_VPN + f = IPv4_VPN case "ipv6-l3vpn", "vpnv6", "vpn-ipv6": - rf = bgp.RF_IPv6_VPN + f = IPv6_VPN case "ipv4-labeled", "ipv4-labelled", "ipv4-mpls": - rf = bgp.RF_IPv4_MPLS + f = IPv4_MPLS case "ipv6-labeled", "ipv6-labelled", "ipv6-mpls": - rf = bgp.RF_IPv6_MPLS + f = IPv6_MPLS case "evpn": - rf = bgp.RF_EVPN + f = EVPN case "encap", "ipv4-encap": - rf = bgp.RF_IPv4_ENCAP + f = IPv4_ENCAP case "ipv6-encap": - rf = bgp.RF_IPv6_ENCAP + f = IPv6_ENCAP case "rtc": - rf = bgp.RF_RTC_UC + f = RTC case "ipv4-flowspec", "ipv4-flow", "flow4": - rf = bgp.RF_FS_IPv4_UC + f = IPv4_FS case "ipv6-flowspec", "ipv6-flow", "flow6": - rf = bgp.RF_FS_IPv6_UC + f = IPv6_FS case "ipv4-l3vpn-flowspec", "ipv4vpn-flowspec", "flowvpn4": - rf = bgp.RF_FS_IPv4_VPN + f = IPv4_VPN_FS case "ipv6-l3vpn-flowspec", "ipv6vpn-flowspec", "flowvpn6": - rf = bgp.RF_FS_IPv6_VPN + f = IPv6_VPN_FS case "l2vpn-flowspec": - rf = bgp.RF_FS_L2_VPN + f = L2_VPN_FS case "opaque": - rf = bgp.RF_OPAQUE + f = OPAQUE case "": - rf = def + f = def default: e = fmt.Errorf("unsupported address family: %s", subOpts.AddressFamily) } - return rf, e + return f, e } func printError(err error) { diff --git a/cmd/gobgp/cmd/global.go b/cmd/gobgp/cmd/global.go index 92f5dfc0..521f6b41 100644 --- a/cmd/gobgp/cmd/global.go +++ b/cmd/gobgp/cmd/global.go @@ -1273,11 +1273,11 @@ func showGlobalRib(args []string) error { } func modPath(resource string, name, modtype string, args []string) error { - rf, err := checkAddressFamily(bgp.RF_IPv4_UC) + f, err := checkAddressFamily(IPv4_UC) if err != nil { return err } - + rf := apiutil.ToRouteFamily(f) path, err := ParsePath(rf, args) if err != nil { cmdstr := "global" @@ -1575,13 +1575,13 @@ func NewGlobalCmd() *cobra.Command { subcmd := &cobra.Command{ Use: CMD_ALL, Run: func(cmd *cobra.Command, args []string) { - family, err := checkAddressFamily(bgp.RouteFamily(0)) + family, err := checkAddressFamily(IPv4_UC) if err != nil { exitWithError(err) } if _, err = client.DeletePath(ctx, &api.DeletePathRequest{ Resource: api.Resource_GLOBAL, - Family: uint32(family), + Family: family, }); err != nil { exitWithError(err) } diff --git a/cmd/gobgp/cmd/monitor.go b/cmd/gobgp/cmd/monitor.go index e1c18e9b..99769679 100644 --- a/cmd/gobgp/cmd/monitor.go +++ b/cmd/gobgp/cmd/monitor.go @@ -114,13 +114,13 @@ func NewMonitorCmd() *cobra.Command { ribCmd := &cobra.Command{ Use: CMD_RIB, Run: func(cmd *cobra.Command, args []string) { - family, err := checkAddressFamily(bgp.RouteFamily(0)) + family, err := checkAddressFamily(IPv4_UC) if err != nil { exitWithError(err) } recver, err := client.MonitorTable(ctx, &api.MonitorTableRequest{ Type: api.Resource_GLOBAL, - Family: uint32(family), + Family: family, Current: current, }) if err != nil { @@ -184,14 +184,14 @@ func NewMonitorCmd() *cobra.Command { } name = args[0] } - family, err := checkAddressFamily(bgp.RouteFamily(0)) + family, err := checkAddressFamily(IPv4_UC) if err != nil { exitWithError(err) } recver, err := client.MonitorTable(ctx, &api.MonitorTableRequest{ Type: api.Resource_ADJ_IN, Name: name, - Family: uint32(family), + Family: family, Current: current, }) if err != nil { diff --git a/cmd/gobgp/cmd/neighbor.go b/cmd/gobgp/cmd/neighbor.go index a3c16c9c..25a53fc5 100644 --- a/cmd/gobgp/cmd/neighbor.go +++ b/cmd/gobgp/cmd/neighbor.go @@ -453,7 +453,8 @@ func showNeighbor(args []string) error { fmt.Println(" Prefix Limits:") first = false } - fmt.Printf(" %s:\tMaximum prefixes allowed %d", bgp.AddressFamilyNameMap[bgp.RouteFamily(limit.Family)], limit.MaxPrefixes) + rf := apiutil.ToRouteFamily(limit.Family) + fmt.Printf(" %s:\tMaximum prefixes allowed %d", bgp.AddressFamilyNameMap[rf], limit.MaxPrefixes) if limit.ShutdownThresholdPct > 0 { fmt.Printf(", Threshold for warning message %d%%\n", limit.ShutdownThresholdPct) } else { @@ -698,7 +699,7 @@ func showValidationInfo(p *api.Path, shownAs map[uint32]struct{}) error { func showRibInfo(r, name string) error { def := addr2AddressFamily(net.ParseIP(name)) if r == CMD_GLOBAL { - def = bgp.RF_IPv4_UC + def = IPv4_UC } family, err := checkAddressFamily(def) if err != nil { @@ -720,7 +721,7 @@ func showRibInfo(r, name string) error { } rsp, err := client.GetTable(ctx, &api.GetTableRequest{ Type: t, - Family: uint32(family), + Family: family, Name: name, }) @@ -760,21 +761,22 @@ func showNeighborRib(r string, name string, args []string) error { def := addr2AddressFamily(net.ParseIP(name)) switch r { case CMD_GLOBAL: - def = bgp.RF_IPv4_UC + def = IPv4_UC showBest = true case CMD_LOCAL: showBest = true case CMD_ADJ_OUT: showAge = false case CMD_VRF: - def = bgp.RF_IPv4_UC + def = IPv4_UC showBest = true } family, err := checkAddressFamily(def) if err != nil { return err } - switch family { + rf := apiutil.ToRouteFamily(family) + switch rf { case bgp.RF_IPv4_MPLS, bgp.RF_IPv6_MPLS, bgp.RF_IPv4_VPN, bgp.RF_IPv6_VPN, bgp.RF_EVPN: showLabel = true } @@ -782,7 +784,7 @@ func showNeighborRib(r string, name string, args []string) error { var filter []*api.TableLookupPrefix if len(args) > 0 { target := args[0] - switch family { + switch rf { case bgp.RF_EVPN: // Uses target as EVPN Route Type string default: @@ -832,7 +834,7 @@ func showNeighborRib(r string, name string, args []string) error { stream, err := client.ListPath(ctx, &api.ListPathRequest{ Type: t, - Family: uint32(family), + Family: family, Name: name, Prefixes: filter, }) @@ -906,7 +908,7 @@ func showNeighborRib(r string, name string, args []string) error { } else { // show RIB var dsts []*api.Destination - switch family { + switch rf { case bgp.RF_IPv4_UC, bgp.RF_IPv6_UC: type d struct { prefix net.IP @@ -1219,8 +1221,12 @@ func modNeighbor(cmdType string, args []string) error { if len(m["family"]) == 1 { peer.AfiSafis = make([]*api.AfiSafi, 0) // for the case of CMD_UPDATE for _, f := range strings.Split(m["family"][0], ",") { - family := config.AfiSafiType(f).ToInt() - peer.AfiSafis = append(peer.AfiSafis, &api.AfiSafi{Config: &api.AfiSafiConfig{Family: uint32(family)}}) + rf, err := bgp.GetRouteFamily(f) + if err != nil { + return err + } + afi, safi := bgp.RouteFamilyToAfiSafi(rf) + peer.AfiSafis = append(peer.AfiSafis, &api.AfiSafi{Config: &api.AfiSafiConfig{Family: apiutil.ToApiFamily(afi, safi)}}) } } if len(m["vrf"]) == 1 { diff --git a/cmd/gobgp/cmd/policy.go b/cmd/gobgp/cmd/policy.go index f94cf17e..81a4618e 100644 --- a/cmd/gobgp/cmd/policy.go +++ b/cmd/gobgp/cmd/policy.go @@ -25,6 +25,8 @@ import ( "strconv" "strings" + "github.com/osrg/gobgp/internal/pkg/apiutil" + "github.com/spf13/cobra" api "github.com/osrg/gobgp/api" @@ -830,9 +832,10 @@ func modCondition(name, op string, args []string) error { case "next-hop-in-list": stmt.Conditions.NextHopInList = args case "afi-safi-in": - afiSafisInList := make([]api.Family, 0, len(args)) + afiSafisInList := make([]*api.Family, 0, len(args)) for _, arg := range args { - afiSafisInList = append(afiSafisInList, api.Family(bgp.AddressFamilyValueMap[arg])) + afi, safi := bgp.RouteFamilyToAfiSafi(bgp.AddressFamilyValueMap[arg]) + afiSafisInList = append(afiSafisInList, apiutil.ToApiFamily(afi, safi)) } stmt.Conditions.AfiSafiIn = afiSafisInList default: diff --git a/cmd/gobgp/cmd/rpki.go b/cmd/gobgp/cmd/rpki.go index a4926088..55aa4b77 100644 --- a/cmd/gobgp/cmd/rpki.go +++ b/cmd/gobgp/cmd/rpki.go @@ -23,7 +23,6 @@ import ( "time" api "github.com/osrg/gobgp/api" - "github.com/osrg/gobgp/pkg/packet/bgp" "github.com/spf13/cobra" ) @@ -86,12 +85,12 @@ func showRPKIServer(args []string) error { } func showRPKITable(args []string) error { - family, err := checkAddressFamily(bgp.RouteFamily(0)) + family, err := checkAddressFamily(IPv4_UC) if err != nil { exitWithError(err) } stream, err := client.ListRpkiTable(ctx, &api.ListRpkiTableRequest{ - Family: uint32(family), + Family: family, }) if err != nil { exitWithError(err) @@ -108,8 +107,7 @@ func showRPKITable(args []string) error { } var format string - afi, _ := bgp.RouteFamilyToAfiSafi(family) - if afi == bgp.AFI_IP { + if family.Afi == api.Family_AFI_IP { format = "%-18s %-6s %-10s %s\n" } else { format = "%-42s %-6s %-10s %s\n" |