From 366179fcc9892b0cbc0bf27da5edbff171d7a444 Mon Sep 17 00:00:00 2001 From: ISHIDA Wataru Date: Wed, 30 Dec 2015 22:02:50 +0900 Subject: cli: add bmp command // add bmp server // by default gobgpd uses tcp port 11019 and sends pre-policy routes $ gobgp bmp add 192.168.0.1 // delete bmp server $ gobgp bmp del 192.168.0.1 // configure bmp server to send post-policy routes $ gobgp bmp add 192.168.0.1 post Signed-off-by: ISHIDA Wataru --- gobgp/cmd/bmp.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++ gobgp/cmd/common.go | 1 + gobgp/cmd/root.go | 3 +- 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 gobgp/cmd/bmp.go diff --git a/gobgp/cmd/bmp.go b/gobgp/cmd/bmp.go new file mode 100644 index 00000000..316f4ded --- /dev/null +++ b/gobgp/cmd/bmp.go @@ -0,0 +1,94 @@ +// Copyright (C) 2015 Nippon Telegraph and Telephone Corporation. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +// implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "fmt" + api "github.com/osrg/gobgp/api" + "github.com/osrg/gobgp/packet" + "github.com/spf13/cobra" + "golang.org/x/net/context" + "net" + "os" + "strconv" +) + +func modBmpServer(cmdType string, args []string) error { + arg := &api.ModBmpArguments{} + if len(args) < 1 { + return fmt.Errorf("usage: gobgp bmp %s [:] [{pre|post|both}]", cmdType) + } + + host, port, err := net.SplitHostPort(args[0]) + if err != nil { + ip := net.ParseIP(args[0]) + if ip == nil { + return nil + } + arg.Address = args[0] + arg.Port = bgp.BMP_DEFAULT_PORT + } else { + arg.Address = host + p, _ := strconv.Atoi(port) + arg.Port = uint32(p) + } + + switch cmdType { + case CMD_ADD: + arg.Operation = api.Operation_ADD + if len(args) > 1 { + switch args[1] { + case "pre": + arg.Type = api.ModBmpArguments_PRE + case "post": + arg.Type = api.ModBmpArguments_POST + case "both": + arg.Type = api.ModBmpArguments_BOTH + default: + return fmt.Errorf("invalid bmp policy type. valid type is {pre|post|both}") + } + } else { + arg.Type = api.ModBmpArguments_PRE + } + case CMD_DEL: + arg.Operation = api.Operation_DEL + } + _, err = client.ModBmp(context.Background(), arg) + return err +} + +func NewBmpCmd() *cobra.Command { + + bmpCmd := &cobra.Command{ + Use: CMD_BMP, + } + + for _, w := range []string{CMD_ADD, CMD_DEL} { + subcmd := &cobra.Command{ + Use: w, + Run: func(cmd *cobra.Command, args []string) { + err := modBmpServer(cmd.Use, args) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + }, + } + bmpCmd.AddCommand(subcmd) + } + + return bmpCmd +} diff --git a/gobgp/cmd/common.go b/gobgp/cmd/common.go index 8dfce3ff..42631c1c 100644 --- a/gobgp/cmd/common.go +++ b/gobgp/cmd/common.go @@ -69,6 +69,7 @@ const ( CMD_ACTION = "action" CMD_UPDATE = "update" CMD_ROTATE = "rotate" + CMD_BMP = "bmp" ) var subOpts struct { diff --git a/gobgp/cmd/root.go b/gobgp/cmd/root.go index 48a87149..6ded72e7 100644 --- a/gobgp/cmd/root.go +++ b/gobgp/cmd/root.go @@ -63,6 +63,7 @@ func NewRootCmd() *cobra.Command { monitorCmd := NewMonitorCmd() mrtCmd := NewMrtCmd() rpkiCmd := NewRPKICmd() - rootCmd.AddCommand(globalCmd, neighborCmd, vrfCmd, policyCmd, monitorCmd, mrtCmd, rpkiCmd) + bmpCmd := NewBmpCmd() + rootCmd.AddCommand(globalCmd, neighborCmd, vrfCmd, policyCmd, monitorCmd, mrtCmd, rpkiCmd, bmpCmd) return rootCmd } -- cgit v1.2.3