Skip to content

Commit

Permalink
small fixes for encap (#362)
Browse files Browse the repository at this point in the history
* small fixes for encap

* lint fixes

* fix
  • Loading branch information
DanG100 authored Feb 23, 2024
1 parent 118035d commit cff01c6
Show file tree
Hide file tree
Showing 20 changed files with 964 additions and 421 deletions.
9 changes: 4 additions & 5 deletions bgp/tests/local_tests/set_attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (
"fmt"
"testing"

"github.com/openconfig/ygot/ygot"

"github.com/openconfig/lemming/bgp"
"github.com/openconfig/lemming/gnmi/oc"
"github.com/openconfig/lemming/gnmi/oc/ocpath"
"github.com/openconfig/ygot/ygot"

valpb "github.com/openconfig/lemming/proto/policyval"
)
Expand All @@ -39,9 +40,7 @@ const (
rejectASPathSetName2 = "reject-as-path-set2"
)

var (
rejectedASPathSet = []string{"64502"}
)
var rejectedASPathSet = []string{"64502"}

func singletonPrefixSetName(route string) string {
return "only-" + route
Expand Down Expand Up @@ -169,7 +168,7 @@ func TestSetAttributes(t *testing.T) {
ExpectedResult: valpb.RouteTestResult_ROUTE_TEST_RESULT_ACCEPT,
}},
},
installPolicies: func(t *testing.T, dut1, dut2, dut3, dut4, dut5 *Device) {
installPolicies: func(t *testing.T, dut1, dut2, _, _, dut5 *Device) {
if debug {
fmt.Println("Installing test policies")
}
Expand Down
8 changes: 4 additions & 4 deletions dataplane/apigen/protogen/protogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func generateCommonTypes(docInfo *docparser.SAIInfo) (string, error) {
common := &protoCommonTmplData{}

// Generate the hand-crafted messages.
rangeInOrder(saiTypeToProto, func(key string, typeInfo saiTypeInfo) error {
rangeInOrder(saiTypeToProto, func(_ string, typeInfo saiTypeInfo) error {
if typeInfo.MessageDef != "" {
common.Messages = append(common.Messages, typeInfo.MessageDef)
}
Expand Down Expand Up @@ -1009,13 +1009,13 @@ var saiTypeToProtoTypeCompound = map[string]func(subType string, xmlInfo *docpar
}
return "repeated " + saiast.TrimSAIName(subType, true, false), true
},
"sai_acl_field_data_t": func(next string, xmlInfo *docparser.SAIInfo) (string, bool) {
"sai_acl_field_data_t": func(_ string, _ *docparser.SAIInfo) (string, bool) {
return "AclFieldData", false
},
"sai_acl_action_data_t": func(next string, xmlInfo *docparser.SAIInfo) (string, bool) {
"sai_acl_action_data_t": func(_ string, _ *docparser.SAIInfo) (string, bool) {
return "AclActionData", false
},
"sai_pointer_t": func(next string, xmlInfo *docparser.SAIInfo) (string, bool) { return "-", false }, // Noop, these are special cases.
"sai_pointer_t": func(_ string, _ *docparser.SAIInfo) (string, bool) { return "-", false }, // Noop, these are special cases.
}

// saiTypeToProtoType returns the protobuf type string for a SAI type.
Expand Down
4 changes: 2 additions & 2 deletions dataplane/forwarding/fwdport/ports/fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ func TestFakeBuild(t *testing.T) {
in := &bytes.Buffer{}
writeHex(t, in, tt.inContents)
out := &bytes.Buffer{}
openFile = func(filename string) (io.Reader, error) {
openFile = func(string) (io.Reader, error) {
return in, tt.openErr
}
createFile = func(filename string) (io.Writer, error) {
createFile = func(string) (io.Writer, error) {
return out, tt.openErr
}
_, err := (fakeBuilder{}).Build(tt.inDesc, nil)
Expand Down
2 changes: 1 addition & 1 deletion dataplane/saiserver/hostif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestSetHostifAttribute(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
getInterface = func(name string) (*net.Interface, error) {
getInterface = func(string) (*net.Interface, error) {
return nil, tt.getInterfaceErr
}
dplane := &fakeSwitchDataplane{}
Expand Down
4 changes: 2 additions & 2 deletions dataplane/saiserver/ports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func TestCreatePort(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
getInterface = func(name string) (*net.Interface, error) {
getInterface = func(string) (*net.Interface, error) {
return nil, tt.getInterfaceErr
}
dplane := &fakeSwitchDataplane{}
Expand Down Expand Up @@ -320,7 +320,7 @@ func TestSetPortAttribute(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
getInterface = func(name string) (*net.Interface, error) {
getInterface = func(string) (*net.Interface, error) {
return nil, tt.getInterfaceErr
}
dplane := &fakeSwitchDataplane{}
Expand Down
18 changes: 12 additions & 6 deletions dataplane/saiserver/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ func (nhg *nextHopGroup) CreateNextHopGroup(_ context.Context, req *saipb.Create

// updateNextHopGroupMember updates the next hop group.
// If m is nil, remove mid from the group(key: nhgid), otherwise add m to group with mid as the key.
func updateNextHopGroupMember(ctx context.Context, nhg *nextHopGroup, nhgid, mid uint64, m *groupMember) (*fwdpb.TableEntryAddReply, error) {
func updateNextHopGroupMember(ctx context.Context, nhg *nextHopGroup, nhgid, mid uint64, m *groupMember) error {
group := nhg.groups[nhgid]
if group == nil {
return nil, status.Errorf(codes.FailedPrecondition, "group %d does not exist", nhgid)
return status.Errorf(codes.FailedPrecondition, "group %d does not exist", nhgid)
}
if m != nil {
group[mid] = m
Expand Down Expand Up @@ -205,7 +205,8 @@ func updateNextHopGroupMember(ctx context.Context, nhg *nextHopGroup, nhgid, mid
Actions: actions,
}},
}
return nhg.dataplane.TableEntryAdd(ctx, entries)
_, err := nhg.dataplane.TableEntryAdd(ctx, entries)
return err
}

// RemoveNextHopGroup removes the next hop group specified in the OID.
Expand Down Expand Up @@ -238,7 +239,7 @@ func (nhg *nextHopGroup) CreateNextHopGroupMember(ctx context.Context, req *saip
nextHop: req.GetNextHopId(),
weight: req.GetWeight(),
}
if _, err := updateNextHopGroupMember(ctx, nhg, nhgid, mid, m); err != nil {
if err := updateNextHopGroupMember(ctx, nhg, nhgid, mid, m); err != nil {
return nil, err
}
return &saipb.CreateNextHopGroupMemberResponse{Oid: mid}, nil
Expand All @@ -262,7 +263,7 @@ func (nhg *nextHopGroup) RemoveNextHopGroupMember(ctx context.Context, req *saip
return nil, err
}

if _, err := updateNextHopGroupMember(ctx, nhg, nhgid, mid, nil); err != nil {
if err := updateNextHopGroupMember(ctx, nhg, nhgid, mid, nil); err != nil {
return nil, err
}
return &saipb.RemoveNextHopGroupMemberResponse{}, nil
Expand Down Expand Up @@ -297,12 +298,17 @@ func (nh *nextHop) CreateNextHop(ctx context.Context, req *saipb.CreateNextHopRe
fwdconfig.Action(fwdconfig.LookupAction(NHActionTable)).Build(),
}
case saipb.NextHopType_NEXT_HOP_TYPE_TUNNEL_ENCAP:
headerID := fwdpb.PacketHeaderId_PACKET_HEADER_ID_IP6
if len(req.Ip) == 4 {
headerID = fwdpb.PacketHeaderId_PACKET_HEADER_ID_IP4
}

actions = []*fwdpb.ActionDesc{
{
ActionType: fwdpb.ActionType_ACTION_TYPE_ENCAP,
Action: &fwdpb.ActionDesc_Encap{
Encap: &fwdpb.EncapActionDesc{
HeaderId: fwdpb.PacketHeaderId_PACKET_HEADER_ID_IP,
HeaderId: headerID,
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions dataplane/saiserver/routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func TestRemoveNextHopGroupMember(t *testing.T) {
req.NextHopGroupId = &r.Oid
resp, err := c.CreateNextHopGroupMember(ctx, req)
if err != nil {
t.Fatal("unexpected error: %v", err)
t.Fatalf("unexpected error: %v", err)
}
// Stores the first member ID.
if memberID == 0 {
Expand Down Expand Up @@ -489,7 +489,7 @@ func TestCreateNextHop(t *testing.T) {
ActionType: fwdpb.ActionType_ACTION_TYPE_ENCAP,
Action: &fwdpb.ActionDesc_Encap{
Encap: &fwdpb.EncapActionDesc{
HeaderId: fwdpb.PacketHeaderId_PACKET_HEADER_ID_IP,
HeaderId: fwdpb.PacketHeaderId_PACKET_HEADER_ID_IP4,
},
},
}, {
Expand Down
20 changes: 20 additions & 0 deletions dataplane/saiserver/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,26 @@ func (sw *saiSwitch) CreateSwitch(ctx context.Context, _ *saipb.CreateSwitchRequ
if err != nil {
return nil, err
}
tunnel := &fwdpb.TableCreateRequest{
ContextId: &fwdpb.ContextId{Id: sw.dataplane.ID()},
Desc: &fwdpb.TableDesc{
TableType: fwdpb.TableType_TABLE_TYPE_EXACT,
TableId: &fwdpb.TableId{ObjectId: &fwdpb.ObjectId{Id: TunnelEncap}},
Actions: []*fwdpb.ActionDesc{{ActionType: fwdpb.ActionType_ACTION_TYPE_DROP}},
Table: &fwdpb.TableDesc_Exact{
Exact: &fwdpb.ExactTableDesc{
FieldIds: []*fwdpb.PacketFieldId{{
Field: &fwdpb.PacketField{
FieldNum: fwdpb.PacketFieldNum_PACKET_FIELD_NUM_TUNNEL_ID,
},
}},
},
},
},
}
if _, err := sw.dataplane.TableCreate(ctx, tunnel); err != nil {
return nil, err
}

cpuPortID, err := sw.port.createCPUPort(ctx)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion dataplane/standalone/lucius/lucius.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func main() {
}

func getLogger() logging.Logger {
return logging.LoggerFunc(func(ctx context.Context, level logging.Level, msg string, fields ...any) {
return logging.LoggerFunc(func(_ context.Context, level logging.Level, msg string, fields ...any) {
switch level {
case logging.LevelDebug:
log.V(1).Info(msg, fields)
Expand Down
2 changes: 1 addition & 1 deletion gnmi/gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ func newSubscribeTargetUpdateStream(s grpc.ServerStream, target string) grpc.Ser
//
// ref: https://www.openconfig.net/docs/gnmi/gnmi-specification/#2221-path-target
func NewSubscribeTargetUpdateInterceptor(target string) func(srv any, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
return func(srv any, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
return func(srv any, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
err := handler(srv, newSubscribeTargetUpdateStream(ss, target))
if err != nil {
fmt.Printf("ERROR in interceptor stream: %v\n", err)
Expand Down
13 changes: 7 additions & 6 deletions gnmi/gnmi_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import (
"testing"

gpb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/lemming/gnmi/gnmiclient"
"github.com/openconfig/lemming/gnmi/oc"
"github.com/openconfig/lemming/gnmi/oc/ocpath"
"github.com/openconfig/ygnmi/ygnmi"
"github.com/openconfig/ygot/ygot"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/local"

"github.com/openconfig/lemming/gnmi/gnmiclient"
"github.com/openconfig/lemming/gnmi/oc"
"github.com/openconfig/lemming/gnmi/oc/ocpath"
)

func BenchmarkGNMISet(b *testing.B) {
Expand Down Expand Up @@ -152,7 +153,7 @@ func BenchmarkGNMISet(b *testing.B) {
_, err := gnmiclient.Replace[*oc.Interface](context.Background(), configClient, ocpath.Root().Interface(name).Config(), &oc.Interface{Description: ygot.String(fmt.Sprintf("iteration %d", i))})
return err
},
op2: func(name string, i int) error {
op2: func(name string, _ int) error {
_, err := gnmiclient.Delete[*oc.Interface](context.Background(), configClient, ocpath.Root().Interface(name).Config())
return err
},
Expand All @@ -162,7 +163,7 @@ func BenchmarkGNMISet(b *testing.B) {
_, err := gnmiclient.Replace(context.Background(), stateClient, ocpath.Root().Interface(name).State(), &oc.Interface{Description: ygot.String(fmt.Sprintf("iteration %d", i))})
return err
},
op2: func(name string, i int) error {
op2: func(name string, _ int) error {
_, err := gnmiclient.Delete(context.Background(), stateClient, ocpath.Root().Interface(name).State())
return err
},
Expand All @@ -181,7 +182,7 @@ func BenchmarkGNMISet(b *testing.B) {
_, err := gnmiclient.Replace[*oc.RoutingPolicy_PolicyDefinition](context.Background(), configClient, ocpath.Root().RoutingPolicy().PolicyDefinition(name).Config(), policy)
return err
},
op2: func(name string, i int) error {
op2: func(name string, _ int) error {
_, err := gnmiclient.Delete[*oc.RoutingPolicy_PolicyDefinition](context.Background(), configClient, ocpath.Root().RoutingPolicy().PolicyDefinition(name).Config())
return err
},
Expand Down
17 changes: 9 additions & 8 deletions gnmi/reconciler/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/openconfig/gnmi/errdiff"
"github.com/openconfig/lemming/gnmi/oc"
"github.com/openconfig/lemming/gnmi/oc/ocpath"
"github.com/openconfig/ygnmi/ygnmi"
"google.golang.org/protobuf/testing/protocmp"

"github.com/openconfig/lemming/gnmi/oc"
"github.com/openconfig/lemming/gnmi/oc/ocpath"

gpb "github.com/openconfig/gnmi/proto/gnmi"
)

Expand All @@ -36,15 +37,15 @@ func TestWithStart(t *testing.T) {
want string
}{{
desc: "single starter",
rec: (&Builder{}).WithStart(func(ctx context.Context, c *ygnmi.Client) error {
rec: (&Builder{}).WithStart(func(context.Context, *ygnmi.Client) error {
return fmt.Errorf("start 1 err")
}).Build(),
want: "start 1 err",
}, {
desc: "2 start funcs",
rec: (&Builder{}).WithStart(func(ctx context.Context, c *ygnmi.Client) error {
rec: (&Builder{}).WithStart(func(context.Context, *ygnmi.Client) error {
return fmt.Errorf("start 1 err")
}).WithStart(func(ctx context.Context, c *ygnmi.Client) error {
}).WithStart(func(context.Context, *ygnmi.Client) error {
return fmt.Errorf("start 2 err")
}).Build(),
want: "start 1 err, start 2 err",
Expand All @@ -67,16 +68,16 @@ func TestWithValidator(t *testing.T) {
wantPaths []ygnmi.PathStruct
}{{
desc: "single validator",
rec: (&Builder{}).WithValidator([]ygnmi.PathStruct{ocpath.Root()}, func(r *oc.Root) error {
rec: (&Builder{}).WithValidator([]ygnmi.PathStruct{ocpath.Root()}, func(*oc.Root) error {
return fmt.Errorf("validator 1")
}).Build(),
want: "validator 1",
wantPaths: []ygnmi.PathStruct{ocpath.Root()},
}, {
desc: "2 validate funcs",
rec: (&Builder{}).WithValidator([]ygnmi.PathStruct{ocpath.Root()}, func(r *oc.Root) error {
rec: (&Builder{}).WithValidator([]ygnmi.PathStruct{ocpath.Root()}, func(*oc.Root) error {
return fmt.Errorf("validator 1")
}).WithValidator([]ygnmi.PathStruct{ocpath.Root().InterfaceAny()}, func(r *oc.Root) error {
}).WithValidator([]ygnmi.PathStruct{ocpath.Root().InterfaceAny()}, func(*oc.Root) error {
return fmt.Errorf("validator 2")
}).Build(),
want: "validator 1, validator 2",
Expand Down
Loading

0 comments on commit cff01c6

Please sign in to comment.