Skip to content

Commit

Permalink
RIF ops (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanG100 authored Dec 19, 2024
1 parent 8e038da commit 13afec2
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 2 deletions.
44 changes: 42 additions & 2 deletions dataplane/saiserver/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,12 +791,25 @@ func (ri *routerInterface) RemoveRouterInterface(ctx context.Context, req *saipb
resp := &saipb.GetRouterInterfaceAttributeResponse{}
err := ri.mgr.PopulateAttributes(&saipb.GetRouterInterfaceAttributeRequest{
Oid: req.GetOid(),
AttrType: []saipb.RouterInterfaceAttr{saipb.RouterInterfaceAttr_ROUTER_INTERFACE_ATTR_PORT_ID},
AttrType: []saipb.RouterInterfaceAttr{saipb.RouterInterfaceAttr_ROUTER_INTERFACE_ATTR_PORT_ID, saipb.RouterInterfaceAttr_ROUTER_INTERFACE_ATTR_TYPE},
}, resp)
if err != nil {
return nil, err
}

var vlanID uint16
if resp.GetAttr().GetType() == saipb.RouterInterfaceType_ROUTER_INTERFACE_TYPE_SUB_PORT {
resp := &saipb.GetRouterInterfaceAttributeResponse{}
err := ri.mgr.PopulateAttributes(&saipb.GetRouterInterfaceAttributeRequest{
Oid: req.GetOid(),
AttrType: []saipb.RouterInterfaceAttr{saipb.RouterInterfaceAttr_ROUTER_INTERFACE_ATTR_OUTER_VLAN_ID},
}, resp)
if err != nil {
return nil, err
}
vlanID = uint16(resp.GetAttr().GetOuterVlanId())
}

nid, err := ri.dataplane.ObjectNID(ctx, &fwdpb.ObjectNIDRequest{
ContextId: &fwdpb.ContextId{Id: ri.dataplane.ID()},
ObjectId: &fwdpb.ObjectId{Id: fmt.Sprint(resp.GetAttr().GetPortId())},
Expand All @@ -807,7 +820,10 @@ func (ri *routerInterface) RemoveRouterInterface(ctx context.Context, req *saipb

_, err = ri.dataplane.TableEntryRemove(ctx, fwdconfig.TableEntryRemoveRequest(ri.dataplane.ID(), inputIfaceTable).
AppendEntry(
fwdconfig.EntryDesc(fwdconfig.ExactEntry(fwdconfig.PacketFieldBytes(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_PACKET_PORT_INPUT).WithUint64(nid.GetNid()))),
fwdconfig.EntryDesc(fwdconfig.ExactEntry(
fwdconfig.PacketFieldBytes(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_PACKET_PORT_INPUT).WithUint64(nid.GetNid()),
fwdconfig.PacketFieldBytes(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_VLAN_TAG).WithBytes(binary.BigEndian.AppendUint16(nil, vlanID)),
)),
).Build())
if err != nil {
return nil, err
Expand Down Expand Up @@ -878,6 +894,30 @@ func (ri *routerInterface) GetRouterInterfaceStats(ctx context.Context, req *sai
return &saipb.GetRouterInterfaceStatsResponse{Values: vals}, nil
}

func (ri *routerInterface) CreateRouterInterfaces(ctx context.Context, req *saipb.CreateRouterInterfacesRequest) (*saipb.CreateRouterInterfacesResponse, error) {
resp := &saipb.CreateRouterInterfacesResponse{}
for _, r := range req.GetReqs() {
id, err := ri.CreateRouterInterface(ctx, r)
if err != nil {
return nil, err
}
resp.Resps = append(resp.Resps, id)
}
return resp, nil
}

func (ri *routerInterface) RemoveRouterInterfaces(ctx context.Context, req *saipb.RemoveRouterInterfacesRequest) (*saipb.RemoveRouterInterfacesResponse, error) {
resp := &saipb.RemoveRouterInterfacesResponse{}
for _, r := range req.GetReqs() {
id, err := ri.RemoveRouterInterface(ctx, r)
if err != nil {
return nil, err
}
resp.Resps = append(resp.Resps, id)
}
return resp, nil
}

// vlanMember contains the info of a VLAN member.
type vlanMember struct {
Oid uint64
Expand Down
93 changes: 93 additions & 0 deletions dataplane/saiserver/routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,99 @@ func TestCreateRouterInterface(t *testing.T) {
}
}

func TestRemoveRouterInterface(t *testing.T) {
tests := []struct {
desc string
req *saipb.RemoveRouterInterfaceRequest
wantReq *fwdpb.TableEntryRemoveRequest
wantErr string
}{{
desc: "unknown type",
req: &saipb.RemoveRouterInterfaceRequest{},
wantErr: "attribute not set",
}, {
desc: "success port - port",
req: &saipb.RemoveRouterInterfaceRequest{
Oid: 10,
},
wantReq: &fwdpb.TableEntryRemoveRequest{
ContextId: &fwdpb.ContextId{Id: "foo"},
TableId: &fwdpb.TableId{ObjectId: &fwdpb.ObjectId{Id: inputIfaceTable}},
Entries: []*fwdpb.EntryDesc{{
Entry: &fwdpb.EntryDesc_Exact{
Exact: &fwdpb.ExactEntryDesc{
Fields: []*fwdpb.PacketFieldBytes{{
FieldId: &fwdpb.PacketFieldId{Field: &fwdpb.PacketField{
FieldNum: fwdpb.PacketFieldNum_PACKET_FIELD_NUM_PACKET_PORT_INPUT,
}},
Bytes: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
}, {
FieldId: &fwdpb.PacketFieldId{Field: &fwdpb.PacketField{
FieldNum: fwdpb.PacketFieldNum_PACKET_FIELD_NUM_VLAN_TAG,
}},
Bytes: binary.BigEndian.AppendUint16(nil, 0),
}},
},
},
}},
},
}, {
desc: "success port - subport",
req: &saipb.RemoveRouterInterfaceRequest{
Oid: 11,
},
wantReq: &fwdpb.TableEntryRemoveRequest{
ContextId: &fwdpb.ContextId{Id: "foo"},
TableId: &fwdpb.TableId{ObjectId: &fwdpb.ObjectId{Id: inputIfaceTable}},
Entries: []*fwdpb.EntryDesc{{
Entry: &fwdpb.EntryDesc_Exact{
Exact: &fwdpb.ExactEntryDesc{
Fields: []*fwdpb.PacketFieldBytes{{
FieldId: &fwdpb.PacketFieldId{Field: &fwdpb.PacketField{
FieldNum: fwdpb.PacketFieldNum_PACKET_FIELD_NUM_PACKET_PORT_INPUT,
}},
Bytes: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
}, {
FieldId: &fwdpb.PacketFieldId{Field: &fwdpb.PacketField{
FieldNum: fwdpb.PacketFieldNum_PACKET_FIELD_NUM_VLAN_TAG,
}},
Bytes: binary.BigEndian.AppendUint16(nil, 100),
}},
},
},
}},
},
}}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
dplane := &fakeSwitchDataplane{
ctx: fwdcontext.New("foo", "foo"),
}
c, mgr, stopFn := newTestRouterInterface(t, dplane)
mgr.StoreAttributes(10, &saipb.RouterInterfaceAttribute{
Type: saipb.RouterInterfaceType_ROUTER_INTERFACE_TYPE_PORT.Enum(),
PortId: proto.Uint64(5),
})
mgr.StoreAttributes(11, &saipb.RouterInterfaceAttribute{
Type: saipb.RouterInterfaceType_ROUTER_INTERFACE_TYPE_SUB_PORT.Enum(),
OuterVlanId: proto.Uint32(100),
PortId: proto.Uint64(5),
})
defer stopFn()
_, gotErr := c.RemoveRouterInterface(context.TODO(), tt.req)
if diff := errdiff.Check(gotErr, tt.wantErr); diff != "" {
t.Fatalf("CreateRouterInterface() unexpected err: %s", diff)
}
if gotErr != nil {
return
}
if d := cmp.Diff(dplane.gotEntryRemoveReqs[0], tt.wantReq, protocmp.Transform()); d != "" {
t.Errorf("CreateRouterInterface() failed: diff(-got,+want)\n:%s", d)
}
})
}
}

func TestCreateHash(t *testing.T) {
tests := []struct {
desc string
Expand Down

0 comments on commit 13afec2

Please sign in to comment.