summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorWataru Ishida <ishida.wataru@lab.ntt.co.jp>2016-11-12 12:50:28 +0000
committerWataru Ishida <ishida.wataru@lab.ntt.co.jp>2016-11-14 02:15:48 +0000
commitee8ce99e2df1f732ef515e20fb3054c0cff0249b (patch)
tree56f8c51a7100e0864edc01396154dcd592e358e3
parent1e22e529c92fe7b2e1576ceea96d827f16a76654 (diff)
client: support neighbor filtering
Signed-off-by: Wataru Ishida <ishida.wataru@lab.ntt.co.jp>
-rw-r--r--client/client.go34
-rw-r--r--gobgp/cmd/neighbor.go49
2 files changed, 49 insertions, 34 deletions
diff --git a/client/client.go b/client/client.go
index 53a822c5..7a7b4606 100644
--- a/client/client.go
+++ b/client/client.go
@@ -97,7 +97,7 @@ func (cli *GoBGPClient) GetServer() (*config.Global, error) {
}, nil
}
-func (cli *GoBGPClient) GetNeighbor() ([]*config.Neighbor, error) {
+func (cli *GoBGPClient) getNeighbor(name string, afi int, vrf string) ([]*config.Neighbor, error) {
ret, err := cli.cli.GetNeighbor(context.Background(), &api.GetNeighborRequest{})
if err != nil {
return nil, err
@@ -106,6 +106,18 @@ func (cli *GoBGPClient) GetNeighbor() ([]*config.Neighbor, error) {
neighbors := make([]*config.Neighbor, 0, len(ret.Peers))
for _, p := range ret.Peers {
+ if name != "" && name != p.Conf.NeighborAddress {
+ continue
+ }
+ if vrf != "" && name != p.Conf.Vrf {
+ continue
+ }
+ if afi > 0 {
+ v6 := net.ParseIP(p.Conf.NeighborAddress).To4() == nil
+ if afi == bgp.AFI_IP && v6 || afi == bgp.AFI_IP6 && !v6 {
+ continue
+ }
+ }
n, err := api.NewNeighborFromAPIStruct(p)
if err != nil {
return nil, err
@@ -115,6 +127,26 @@ func (cli *GoBGPClient) GetNeighbor() ([]*config.Neighbor, error) {
return neighbors, nil
}
+func (cli *GoBGPClient) ListNeighbor() ([]*config.Neighbor, error) {
+ return cli.getNeighbor("", 0, "")
+}
+
+func (cli *GoBGPClient) ListNeighborByTransport(afi int) ([]*config.Neighbor, error) {
+ return cli.getNeighbor("", afi, "")
+}
+
+func (cli *GoBGPClient) ListNeighborByVRF(vrf string) ([]*config.Neighbor, error) {
+ return cli.getNeighbor("", 0, vrf)
+}
+
+func (cli *GoBGPClient) GetNeighbor(name string) (*config.Neighbor, error) {
+ ns, err := cli.getNeighbor(name, 0, "")
+ if err != nil {
+ return nil, err
+ }
+ return ns[0], nil
+}
+
func (cli *GoBGPClient) AddNeighbor(c *config.Neighbor) error {
peer := api.NewPeerFromConfigStruct(c)
_, err := cli.cli.AddNeighbor(context.Background(), &api.AddNeighborRequest{peer})
diff --git a/gobgp/cmd/neighbor.go b/gobgp/cmd/neighbor.go
index f161b12d..f533f946 100644
--- a/gobgp/cmd/neighbor.go
+++ b/gobgp/cmd/neighbor.go
@@ -30,44 +30,27 @@ import (
)
func getNeighbors(vrf string) (neighbors, error) {
- ns, e := client.GetNeighbor()
- if e != nil {
- fmt.Println(e)
- return nil, e
- }
- var m neighbors
- for _, n := range ns {
- if neighborsOpts.Transport != "" {
- addr, _ := net.ResolveIPAddr("ip", n.Config.NeighborAddress)
- if addr.IP.To4() != nil {
- if neighborsOpts.Transport != "ipv4" {
- continue
- }
- } else {
- if neighborsOpts.Transport != "ipv6" {
- continue
- }
- }
- }
- if vrf != "" && n.Config.Vrf != vrf {
- continue
+ if vrf != "" {
+ n, err := client.ListNeighborByVRF(vrf)
+ return neighbors(n), err
+ } else if t := neighborsOpts.Transport; t != "" {
+ switch t {
+ case "ipv4":
+ n, err := client.ListNeighborByTransport(bgp.AFI_IP)
+ return neighbors(n), err
+ case "ipv6":
+ n, err := client.ListNeighborByTransport(bgp.AFI_IP6)
+ return neighbors(n), err
+ default:
+ return nil, fmt.Errorf("invalid transport: %s", t)
}
- m = append(m, n)
}
- return m, nil
+ n, err := client.ListNeighbor()
+ return neighbors(n), err
}
func getNeighbor(name string) (*config.Neighbor, error) {
- l, e := getNeighbors("")
- if e != nil {
- return nil, e
- }
- for _, p := range l {
- if p.Config.NeighborAddress == name || p.Config.NeighborAddress == name {
- return p, nil
- }
- }
- return nil, fmt.Errorf("Neighbor %v doesn't exist.", name)
+ return client.GetNeighbor(name)
}
func showNeighbors(vrf string) error {