diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2016-03-16 18:08:43 +0900 |
---|---|---|
committer | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2016-03-17 10:47:11 +0900 |
commit | a5cc2b139733f35aa9733b081e0c12e1bb3505d4 (patch) | |
tree | 11bdb7cb88f1f655bb19998aa71e929c331ae3e8 | |
parent | 74b6b11a029a585a6e22b8a4eb59c4d87504fa5e (diff) |
cli: enhance global command
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
-rw-r--r-- | config/default.go | 4 | ||||
-rw-r--r-- | docs/sources/cli-command-syntax.md | 13 | ||||
-rw-r--r-- | gobgp/cmd/common.go | 24 | ||||
-rw-r--r-- | gobgp/cmd/common_test.go | 36 | ||||
-rw-r--r-- | gobgp/cmd/global.go | 54 | ||||
-rw-r--r-- | server/server.go | 16 |
6 files changed, 133 insertions, 14 deletions
diff --git a/config/default.go b/config/default.go index 803c8a2e..5c9248ad 100644 --- a/config/default.go +++ b/config/default.go @@ -48,6 +48,10 @@ func SetDefaultConfigValues(v *viper.Viper, b *Bgp) error { b.Global.ListenConfig.Port = bgp.BGP_PORT } + if len(b.Global.ListenConfig.LocalAddressList) == 0 { + b.Global.ListenConfig.LocalAddressList = []string{"0.0.0.0", "::"} + } + for idx, server := range b.BmpServers { if server.Config.Port == 0 { server.Config.Port = bgp.BMP_DEFAULT_PORT diff --git a/docs/sources/cli-command-syntax.md b/docs/sources/cli-command-syntax.md index 5376d7fa..bd34e486 100644 --- a/docs/sources/cli-command-syntax.md +++ b/docs/sources/cli-command-syntax.md @@ -17,7 +17,18 @@ gobgp has six subcommands. ## 1. <a name="global"> global subcommand -### 1.1. Operations for Global-Rib - add/del/show - +### 1.1 Global Configuration +#### syntax +```shell +# configure global setting and start acting as bgp daemon +% gobgp global as <VALUE> router-id <VALUE> [listen-port <VALUE>] [listen-addresses <VALUE>...] [mpls-label-min <VALUE>] [mpls-label-max <VALUE>] [collector] +# delete global setting and stop acting as bgp daemon (all peer sessions will be closed) +% gobgp global del all +# show global setting +% gobgp global +``` + +### 1.2. Operations for Global-Rib - add/del/show - #### - syntax ```shell # add Route diff --git a/gobgp/cmd/common.go b/gobgp/cmd/common.go index 30640940..c17185ad 100644 --- a/gobgp/cmd/common.go +++ b/gobgp/cmd/common.go @@ -242,6 +242,30 @@ func (p paths) Less(i, j int) bool { return strings.Less(0, 1) } +func extractReserved(args, keys []string) map[string][]string { + m := make(map[string][]string, len(keys)) + var k string + isReserved := func(s string) bool { + for _, r := range keys { + if s == r { + return true + } + } + return false + } + for _, arg := range args { + if isReserved(arg) { + k = arg + m[k] = make([]string, 0, 1) + } else if k == "" { + m[k] = []string{arg} + } else { + m[k] = append(m[k], arg) + } + } + return m +} + type PeerConf struct { RemoteIp net.IP `json:"remote_ip,omitempty"` Id net.IP `json:"id,omitempty"` diff --git a/gobgp/cmd/common_test.go b/gobgp/cmd/common_test.go new file mode 100644 index 00000000..ed4bfe4a --- /dev/null +++ b/gobgp/cmd/common_test.go @@ -0,0 +1,36 @@ +// Copyright (C) 2016 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" + "github.com/stretchr/testify/assert" + "strings" + "testing" +) + +func Test_ExtractReserved(t *testing.T) { + assert := assert.New(t) + args := strings.Split("10 rt 100:100 med 10 nexthop 10.0.0.1 aigp metric 10 local-pref 100", " ") + keys := []string{"rt", "med", "nexthop", "aigp", "local-pref"} + m := extractReserved(args, keys) + fmt.Println(m) + assert.True(len(m["rt"]) == 1) + assert.True(len(m["med"]) == 1) + assert.True(len(m["nexthop"]) == 1) + assert.True(len(m["aigp"]) == 2) + assert.True(len(m["local-pref"]) == 1) +} diff --git a/gobgp/cmd/global.go b/gobgp/cmd/global.go index ebdcbd95..1b1ca8ed 100644 --- a/gobgp/cmd/global.go +++ b/gobgp/cmd/global.go @@ -16,6 +16,7 @@ package cmd import ( + "encoding/json" "fmt" api "github.com/osrg/gobgp/api" "github.com/osrg/gobgp/packet" @@ -773,28 +774,69 @@ func showGlobalConfig(args []string) error { if err != nil { return err } + if globalOpts.Json { + j, _ := json.Marshal(g) + fmt.Println(string(j)) + return nil + } fmt.Println("AS: ", g.As) fmt.Println("Router-ID:", g.RouterId) + if len(g.ListenAddresses) > 0 { + fmt.Printf("Listening Port: %d, Addresses: %s\n", g.ListenPort, strings.Join(g.ListenAddresses, ", ")) + } + fmt.Printf("MPLS Label Range: %d..%d\n", g.MplsLabelMin, g.MplsLabelMax) + if g.Collector { + fmt.Println("Running in Collector Mode") + } return nil } func modGlobalConfig(args []string) error { - if len(args) != 4 || args[0] != "as" || args[2] != "router-id" { - return fmt.Errorf("usage: gobgp global as <asn> router-id <route-id>") + m := extractReserved(args, []string{"as", "router-id", "listen-port", + "listen-addresses", "mpls-label-min", "mpls-label-max", "collector"}) + + if len(m["as"]) != 1 || len(m["router-id"]) != 1 { + return fmt.Errorf("usage: gobgp global as <VALUE> router-id <VALUE> [listen-port <VALUE>] [listen-addresses <VALUE>...] [mpls-label-min <VALUE>] [mpls-label-max <VALUE>] [collector]") } - asn, err := strconv.Atoi(args[1]) + asn, err := strconv.Atoi(m["as"][0]) if err != nil { return err } - id := net.ParseIP(args[3]) + id := net.ParseIP(m["router-id"][0]) if id.To4() == nil { return fmt.Errorf("invalid router-id format") } + var port int + if len(m["listen-port"]) > 0 { + port, err = strconv.Atoi(m["listen-port"][0]) + if err != nil { + return err + } + } + var min, max int + if len(m["mpls-label-min"]) > 0 { + min, err = strconv.Atoi(m["mpls-label-min"][0]) + if err != nil { + return err + } + } + if len(m["mpls-label-man"]) > 0 { + min, err = strconv.Atoi(m["mpls-label-man"][0]) + if err != nil { + return err + } + } + _, collector := m["collector"] _, err = client.ModGlobalConfig(context.Background(), &api.ModGlobalConfigArguments{ Operation: api.Operation_ADD, Global: &api.Global{ - As: uint32(asn), - RouterId: args[3], + As: uint32(asn), + RouterId: id.String(), + ListenPort: int32(port), + ListenAddresses: m["listen-addresses"], + MplsLabelMin: uint32(min), + MplsLabelMax: uint32(max), + Collector: collector, }, }) return err diff --git a/server/server.go b/server/server.go index d5cc8882..846931c4 100644 --- a/server/server.go +++ b/server/server.go @@ -1697,11 +1697,7 @@ func (server *BgpServer) handleModConfig(grpcReq *GrpcRequest) error { if c.ListenConfig.Port > 0 { acceptCh := make(chan *net.TCPConn, 4096) - list := []string{"0.0.0.0", "::"} - if len(c.ListenConfig.LocalAddressList) > 0 { - list = c.ListenConfig.LocalAddressList - } - for _, addr := range list { + for _, addr := range c.ListenConfig.LocalAddressList { l, err := listenAndAccept(addr, uint32(c.ListenConfig.Port), acceptCh) if err != nil { return err @@ -1805,10 +1801,16 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { switch grpcReq.RequestType { case REQ_GLOBAL_CONFIG: + g := server.bgpConfig.Global result := &GrpcResponse{ Data: &api.Global{ - As: server.bgpConfig.Global.Config.As, - RouterId: server.bgpConfig.Global.Config.RouterId, + As: g.Config.As, + RouterId: g.Config.RouterId, + ListenPort: g.ListenConfig.Port, + ListenAddresses: g.ListenConfig.LocalAddressList, + MplsLabelMin: g.MplsLabelRange.MinLabel, + MplsLabelMax: g.MplsLabelRange.MaxLabel, + Collector: g.Collector.Enabled, }, } grpcReq.ResponseCh <- result |