Skip to content

Commit

Permalink
Show file tree
Hide file tree
Showing 6 changed files with 602 additions and 485 deletions.
936 changes: 473 additions & 463 deletions api/gobgp.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions api/gobgp.proto
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ message NexthopAction {
string address = 1;
bool self = 2;
bool unchanged = 3;
bool peer_address = 4;
}

message LocalPrefAction { uint32 value = 1; }
Expand Down
10 changes: 6 additions & 4 deletions cmd/gobgp/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ func prettyString(v interface{}) string {
case *api.LocalPrefAction:
return fmt.Sprintf("%d", a.Value)
case *api.NexthopAction:
if a.Self {
switch {
case a.Self:
return "self"
}
if a.Unchanged {
case a.Unchanged:
return "unchanged"
case a.PeerAddress:
return "peer-address"
}
return a.Address
case *api.AsPrependAction:
Expand Down Expand Up @@ -944,7 +946,7 @@ func modAction(name, op string, args []string) error {
case "next-hop":
stmt.Actions.Nexthop = &api.NexthopAction{}
if len(args) != 1 {
return fmt.Errorf("%s next-hop { <value> | self | unchanged }", usage)
return fmt.Errorf("%s next-hop { <value> | self | unchanged | peer-address }", usage)
}
stmt.Actions.Nexthop.Address = args[0]
}
Expand Down
40 changes: 28 additions & 12 deletions internal/pkg/table/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2693,23 +2693,29 @@ func NewAsPathPrependAction(action oc.SetAsPathPrepend) (*AsPathPrependAction, e
}

type NexthopAction struct {
value net.IP
self bool
unchanged bool
value net.IP
self bool
peerAddress bool
unchanged bool
}

func (a *NexthopAction) Type() ActionType {
return ACTION_NEXTHOP
}

func (a *NexthopAction) Apply(path *Path, options *PolicyOptions) (*Path, error) {
if a.self {
switch {
case a.self:
if options != nil && options.Info != nil && options.Info.LocalAddress != nil {
path.SetNexthop(options.Info.LocalAddress)
}
return path, nil
}
if a.unchanged {
case a.peerAddress:
if options != nil && options.Info != nil && options.Info.Address != nil {
path.SetNexthop(options.Info.Address)
}
return path, nil
case a.unchanged:
if options != nil && options.OldNextHop != nil {
path.SetNexthop(options.OldNextHop)
}
Expand All @@ -2720,10 +2726,12 @@ func (a *NexthopAction) Apply(path *Path, options *PolicyOptions) (*Path, error)
}

func (a *NexthopAction) ToConfig() oc.BgpNextHopType {
if a.self {
switch {
case a.self:
return oc.BgpNextHopType("self")
}
if a.unchanged {
case a.peerAddress:
return oc.BgpNextHopType("peer-address")
case a.unchanged:
return oc.BgpNextHopType("unchanged")
}
return oc.BgpNextHopType(a.value.String())
Expand All @@ -2745,6 +2753,10 @@ func NewNexthopAction(c oc.BgpNextHopType) (*NexthopAction, error) {
return &NexthopAction{
self: true,
}, nil
case "peer-address":
return &NexthopAction{
peerAddress: true,
}, nil
case "unchanged":
return &NexthopAction{
unchanged: true,
Expand Down Expand Up @@ -4228,12 +4240,16 @@ func toStatementApi(s *oc.Statement) *api.Statement {
return nil
}

if string(s.Actions.BgpActions.SetNextHop) == "self" {
switch string(s.Actions.BgpActions.SetNextHop) {
case "self":
return &api.NexthopAction{
Self: true,
}
}
if string(s.Actions.BgpActions.SetNextHop) == "unchanged" {
case "peer-address":
return &api.NexthopAction{
PeerAddress: true,
}
case "unchanged":
return &api.NexthopAction{
Unchanged: true,
}
Expand Down
82 changes: 82 additions & 0 deletions internal/pkg/table/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/osrg/gobgp/v3/pkg/config/oc"
"github.com/osrg/gobgp/v3/pkg/packet/bgp"

Expand Down Expand Up @@ -654,6 +655,87 @@ func TestPolicyMatchAndRejectNextHop(t *testing.T) {
assert.Equal(t, newPath, path)
}

func TestSetNextHop(t *testing.T) {
// create path
peer := &PeerInfo{AS: 65001, Address: net.ParseIP("10.0.0.2"), LocalAddress: net.ParseIP("20.0.0.1")}
origin := bgp.NewPathAttributeOrigin(0)
aspathParam := []bgp.AsPathParamInterface{bgp.NewAsPathParam(2, []uint16{65001})}
aspath := bgp.NewPathAttributeAsPath(aspathParam)
nexthop := bgp.NewPathAttributeNextHop("10.0.0.5")
med := bgp.NewPathAttributeMultiExitDisc(0)
pathAttributes := []bgp.PathAttributeInterface{origin, aspath, nexthop, med}
nlri := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(24, "10.10.0.101")}
updateMsg := bgp.NewBGPUpdateMessage(nil, pathAttributes, nlri)
path := ProcessMessage(updateMsg, peer, time.Now())[0]

// create policy
ps := createPrefixSet("ps", "10.10.0.0/16", "21..24")
ns := createNeighborSet("ns", "10.0.0.2")
ds := oc.DefinedSets{}
ds.PrefixSets = []oc.PrefixSet{ps}
ds.NeighborSets = []oc.NeighborSet{ns}

t.Run("custom", func(t *testing.T) {
s1 := createStatement("statement1", "ps", "ns", true)
s1.Actions.BgpActions.SetNextHop = oc.BgpNextHopType("10.2.2.2")
s1.Actions.RouteDisposition = oc.ROUTE_DISPOSITION_NONE
s2 := createStatement("statement2", "ps", "ns", true)
s2.Conditions.BgpConditions.NextHopInList = []string{"10.2.2.2"}
pd := createPolicyDefinition("pd1", s1, s2)
pl := createRoutingPolicy(ds, pd)

r := NewRoutingPolicy(logger)
err := r.reload(pl)
assert.Nil(t, err)
pType, newPath := r.policyMap["pd1"].Apply(logger, path, &PolicyOptions{Info: peer})
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
path.SetNexthop(net.ParseIP("10.2.2.2"))
if diff := cmp.Diff(newPath, path); diff != "" {
t.Errorf("(-want, +got):\n%s", diff)
}
})

t.Run("self", func(t *testing.T) {
s1 := createStatement("statement1", "ps", "ns", true)
s1.Actions.BgpActions.SetNextHop = oc.BgpNextHopType("self")
s1.Actions.RouteDisposition = oc.ROUTE_DISPOSITION_NONE
s2 := createStatement("statement2", "ps", "ns", true)
s2.Conditions.BgpConditions.NextHopInList = []string{"20.0.0.1"}
pd := createPolicyDefinition("pd1", s1, s2)
pl := createRoutingPolicy(ds, pd)

r := NewRoutingPolicy(logger)
err := r.reload(pl)
assert.Nil(t, err)
pType, newPath := r.policyMap["pd1"].Apply(logger, path, &PolicyOptions{Info: peer})
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
path.SetNexthop(net.ParseIP("20.0.0.1"))
if diff := cmp.Diff(newPath, path); diff != "" {
t.Errorf("(-want, +got):\n%s", diff)
}
})

t.Run("peer-address", func(t *testing.T) {
s1 := createStatement("statement1", "ps", "ns", true)
s1.Actions.BgpActions.SetNextHop = oc.BgpNextHopType("peer-address")
s1.Actions.RouteDisposition = oc.ROUTE_DISPOSITION_NONE
s2 := createStatement("statement2", "ps", "ns", true)
s2.Conditions.BgpConditions.NextHopInList = []string{"10.0.0.2"}
pd := createPolicyDefinition("pd1", s1, s2)
pl := createRoutingPolicy(ds, pd)

r := NewRoutingPolicy(logger)
err := r.reload(pl)
assert.Nil(t, err)
pType, newPath := r.policyMap["pd1"].Apply(logger, path, &PolicyOptions{Info: peer})
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
path.SetNexthop(net.ParseIP("10.0.0.2"))
if diff := cmp.Diff(newPath, path); diff != "" {
t.Errorf("(-want, +got):\n%s", diff)
}
})
}

func TestAsPathLengthConditionWithOtherCondition(t *testing.T) {
// setup
// create path
Expand Down
18 changes: 12 additions & 6 deletions pkg/server/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1257,15 +1257,19 @@ func toStatementApi(s *oc.Statement) *api.Statement {
return nil
}

if string(s.Actions.BgpActions.SetNextHop) == "self" {
switch string(s.Actions.BgpActions.SetNextHop) {
case "self":
return &api.NexthopAction{
Self: true,
}
}
if string(s.Actions.BgpActions.SetNextHop) == "unchanged" {
case "unchanged":
return &api.NexthopAction{
Unchanged: true,
}
case "peer-address":
return &api.NexthopAction{
PeerAddress: true,
}
}
return &api.NexthopAction{
Address: string(s.Actions.BgpActions.SetNextHop),
Expand Down Expand Up @@ -1606,11 +1610,13 @@ func newNexthopActionFromApiStruct(a *api.NexthopAction) (*table.NexthopAction,
}
return table.NewNexthopAction(oc.BgpNextHopType(
func() string {
if a.Self {
switch {
case a.Self:
return "self"
}
if a.Unchanged {
case a.Unchanged:
return "unchanged"
case a.PeerAddress:
return "peer-address"
}
return a.Address
}(),
Expand Down

0 comments on commit 0148e2d

Please sign in to comment.