diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2015-10-19 15:59:08 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-10-20 10:33:10 +0900 |
commit | 485cab56ed4c8f7ec43edf4204493f60d9519adf (patch) | |
tree | c7b7d18142ca792ec9f6eed0e2b379291800e192 | |
parent | f634b5035729485105d54c25198d36542337aea5 (diff) |
cli: support configure neighbor/global policy assignment
- assign p0, p1 to 10.0.0.1's in-policy
$ gobgp neighbor 10.0.0.1 policy in add p0 p1
- remove p0 from 10.0.0.1's in-policy
$ gobgp neighbor 10.0.0.1 policy in del p0
- set different policies
$ gobgp neighbor 10.0.0.1 policy in set p2 p3
- remove all
$ gobgp neighbor 10.0.0.1 policy in del
- assign p0, p1 to global export-policy
$ gobgp global policy export add p0 p1
- remove p0 from global export-policy
$ gobgp global policy export del p0
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
-rw-r--r-- | gobgp/cmd/global.go | 15 | ||||
-rw-r--r-- | gobgp/cmd/neighbor.go | 137 |
2 files changed, 87 insertions, 65 deletions
diff --git a/gobgp/cmd/global.go b/gobgp/cmd/global.go index 4af4c44a..d3a35b49 100644 --- a/gobgp/cmd/global.go +++ b/gobgp/cmd/global.go @@ -669,6 +669,21 @@ func NewGlobalCmd() *cobra.Command { } }, } + + for _, w := range []string{CMD_ADD, CMD_DEL, CMD_SET} { + subcmd := &cobra.Command{ + Use: w, + Run: func(subcmd *cobra.Command, args []string) { + err := modNeighborPolicy(nil, cmd.Use, subcmd.Use, args) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + }, + } + cmd.AddCommand(subcmd) + } + policyCmd.AddCommand(cmd) } diff --git a/gobgp/cmd/neighbor.go b/gobgp/cmd/neighbor.go index 44a31445..91db2e0e 100644 --- a/gobgp/cmd/neighbor.go +++ b/gobgp/cmd/neighbor.go @@ -610,26 +610,26 @@ func stateChangeNeighbor(cmd string, remoteIP string, args []string) error { } func showNeighborPolicy(remoteIP net.IP, policyType string) error { - p := &api.ApplyPolicy{} + var typ api.PolicyType switch strings.ToLower(policyType) { case "in": - p.Type = api.PolicyType_IN + typ = api.PolicyType_IN case "import": - p.Type = api.PolicyType_IMPORT + typ = api.PolicyType_IMPORT case "export": - p.Type = api.PolicyType_EXPORT + typ = api.PolicyType_EXPORT } r := api.Resource_LOCAL if remoteIP == nil { r = api.Resource_GLOBAL } - arg := &api.PolicyArguments{ - NeighborAddress: remoteIP.String(), - ApplyPolicy: p, - Resource: r, + arg := &api.PolicyAssignment{ + Name: remoteIP.String(), + Resource: r, + Type: typ, } - ap, e := client.GetNeighborPolicy(context.Background(), arg) + ap, e := client.GetPolicyAssignment(context.Background(), arg) if e != nil { return e } @@ -642,80 +642,87 @@ func showNeighborPolicy(remoteIP net.IP, policyType string) error { fmt.Printf("Default: %s\n", ap.Default) for _, p := range ap.Policies { - fmt.Printf(" PolicyName %s:\n", p) + fmt.Printf("Name %s:\n", p.Name) + printPolicy(4, p) } return nil } -func parsePolicy(pNames string) []*api.Policy { - pList := strings.Split(pNames, ",") - policyList := make([]*api.Policy, 0, len(pList)) - for _, p := range pList { - if p != "" { - policy := &api.Policy{ - Name: p, +func extractDefaultAction(args []string) ([]string, api.RouteAction, error) { + for idx, arg := range args { + if arg == "default" { + if len(args) < (idx + 2) { + return nil, api.RouteAction_NONE, fmt.Errorf("specify default action [accept|reject]") + } + typ := args[idx+1] + switch typ { + case "accept": + return append(args[:idx], args[idx+2:]...), api.RouteAction_ACCEPT, nil + case "reject": + return append(args[:idx], args[idx+2:]...), api.RouteAction_REJECT, nil + default: + return nil, api.RouteAction_NONE, fmt.Errorf("invalid default action") } - policyList = append(policyList, policy) } } - return policyList + return args, api.RouteAction_NONE, nil } -func modNeighborPolicy(remoteIP net.IP, policyType, cmdType string, eArg []string) error { - var operation api.Operation - p := &api.ApplyPolicy{} +func modNeighborPolicy(remoteIP net.IP, policyType, cmdType string, args []string) error { + var typ api.PolicyType switch strings.ToLower(policyType) { case "in": - p.Type = api.PolicyType_IN + typ = api.PolicyType_IN case "import": - p.Type = api.PolicyType_IMPORT + typ = api.PolicyType_IMPORT case "export": - p.Type = api.PolicyType_EXPORT - } - - switch cmdType { - case CMD_ADD: - if len(eArg) < 3 { - return fmt.Errorf("Usage: gobgp neighbor <ipaddr> policy %s %s [<policy name>...] [default {%s|%s}]", policyType, cmdType, "accept", "reject") - } - switch eArg[1] { - case "accept": - p.Default = api.RouteAction_ACCEPT - case "reject": - p.Default = api.RouteAction_REJECT - default: - return fmt.Errorf("Usage: gobgp neighbor <ipaddr> policy %s %s [<policy name>...] [default {%s|%s}]", policyType, cmdType, "accept", "reject") - } - p.Policies = []string{eArg[0]} - operation = api.Operation_ADD - case CMD_DEL: - operation = api.Operation_DEL - } - arg := &api.PolicyArguments{ - Resource: api.Resource_POLICY_ROUTEPOLICY, - Operation: operation, - NeighborAddress: remoteIP.String(), - Name: eArg[0], - ApplyPolicy: p, + typ = api.PolicyType_EXPORT } - stream, err := client.ModNeighborPolicy(context.Background()) - if err != nil { - return err + r := api.Resource_LOCAL + usage := fmt.Sprintf("usage: gobgp neighbor %s policy %s %s", remoteIP, policyType, cmdType) + if remoteIP == nil { + r = api.Resource_GLOBAL + usage = fmt.Sprintf("usage: gobgp global policy %s %s", policyType, cmdType) } - err = stream.Send(arg) - if err != nil { - return err + + arg := &api.ModPolicyAssignmentArguments{ + Assignment: &api.PolicyAssignment{ + Type: typ, + Resource: r, + Name: remoteIP.String(), + }, } - stream.CloseSend() - res, e := stream.Recv() - if e != nil { - return e + switch cmdType { + case CMD_ADD, CMD_SET: + if len(args) < 1 { + return fmt.Errorf("%s <policy name>... [default {%s|%s}]", usage, "accept", "reject") + } + var err error + var def api.RouteAction + args, def, err = extractDefaultAction(args) + if err != nil { + return fmt.Errorf("%s\n%s <policy name>... [default {%s|%s}]", err, usage, "accept", "reject") + } + if cmdType == CMD_ADD { + arg.Operation = api.Operation_ADD + } else { + arg.Operation = api.Operation_REPLACE + } + arg.Assignment.Default = def + case CMD_DEL: + arg.Operation = api.Operation_DEL + if len(args) == 0 { + arg.Operation = api.Operation_DEL_ALL + } } - if res.Code != api.Error_SUCCESS { - return fmt.Errorf("error: code: %d, msg: %s", res.Code, res.Msg) + ps := make([]*api.Policy, 0, len(args)) + for _, name := range args { + ps = append(ps, &api.Policy{Name: name}) } - return nil + arg.Assignment.Policies = ps + _, err := client.ModPolicyAssignment(context.Background(), arg) + return err } func NewNeighborCmd() *cobra.Command { @@ -787,7 +794,7 @@ func NewNeighborCmd() *cobra.Command { }, } - for _, w := range []string{CMD_ADD, CMD_DEL} { + for _, w := range []string{CMD_ADD, CMD_DEL, CMD_SET} { subcmd := &cobra.Command{ Use: w, Run: func(subcmd *cobra.Command, args []string) { |