github.com/osrg/gobgp@v2.0.0+incompatible/cmd/gobgp/bmp.go (about) 1 // Copyright (C) 2015 Nippon Telegraph and Telephone Corporation. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 // implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package main 17 18 import ( 19 "fmt" 20 "net" 21 "strconv" 22 23 api "github.com/osrg/gobgp/api" 24 "github.com/osrg/gobgp/pkg/packet/bmp" 25 "github.com/spf13/cobra" 26 ) 27 28 func modBmpServer(cmdType string, args []string) error { 29 if len(args) < 1 { 30 return fmt.Errorf("usage: gobgp bmp %s <addr>[:<port>] [{pre|post|both|local-rib|all}]", cmdType) 31 } 32 33 var address string 34 port := uint32(bmp.BMP_DEFAULT_PORT) 35 if host, p, err := net.SplitHostPort(args[0]); err != nil { 36 ip := net.ParseIP(args[0]) 37 if ip == nil { 38 return nil 39 } 40 address = args[0] 41 } else { 42 address = host 43 // Note: BmpServerConfig.Port is uint32 type, but the TCP/UDP port is 44 // 16-bit length. 45 pn, _ := strconv.ParseUint(p, 10, 16) 46 port = uint32(pn) 47 } 48 49 var err error 50 switch cmdType { 51 case cmdAdd: 52 statisticsTimeout := 0 53 if bmpOpts.StatisticsTimeout >= 0 && bmpOpts.StatisticsTimeout <= 65535 { 54 statisticsTimeout = bmpOpts.StatisticsTimeout 55 } else { 56 return fmt.Errorf("invalid statistics-timeout value. it must be in the range 0-65535. default value is 0 and means disabled.") 57 } 58 59 policyType := api.AddBmpRequest_PRE 60 if len(args) > 1 { 61 switch args[1] { 62 case "post": 63 policyType = api.AddBmpRequest_POST 64 case "both": 65 policyType = api.AddBmpRequest_BOTH 66 case "local-rib": 67 policyType = api.AddBmpRequest_LOCAL 68 case "all": 69 policyType = api.AddBmpRequest_ALL 70 default: 71 return fmt.Errorf("invalid bmp policy type. valid type is {pre|post|both|local-rib|all}") 72 } 73 } 74 _, err = client.AddBmp(ctx, &api.AddBmpRequest{ 75 Address: address, 76 Port: port, 77 Policy: policyType, 78 StatisticsTimeout: int32(statisticsTimeout), 79 }) 80 case cmdDel: 81 _, err = client.DeleteBmp(ctx, &api.DeleteBmpRequest{ 82 Address: address, 83 Port: port, 84 }) 85 } 86 return err 87 } 88 89 func newBmpCmd() *cobra.Command { 90 bmpCmd := &cobra.Command{ 91 Use: cmdBMP, 92 } 93 94 for _, w := range []string{cmdAdd, cmdDel} { 95 subcmd := &cobra.Command{ 96 Use: w, 97 Run: func(cmd *cobra.Command, args []string) { 98 err := modBmpServer(cmd.Use, args) 99 if err != nil { 100 exitWithError(err) 101 } 102 }, 103 } 104 if w == cmdAdd { 105 subcmd.PersistentFlags().IntVarP(&bmpOpts.StatisticsTimeout, "statistics-timeout", "s", 0, "Timeout of statistics report") 106 } 107 bmpCmd.AddCommand(subcmd) 108 } 109 110 return bmpCmd 111 }