Skip to content

Commit

Permalink
Support route metadata (#493)
Browse files Browse the repository at this point in the history
* Support route metadata

* feedback
  • Loading branch information
DanG100 authored Nov 1, 2024
1 parent 6a07d7b commit f63a2c1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
7 changes: 7 additions & 0 deletions dataplane/saiserver/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,13 @@ func (a *acl) createAclEntryFields(req *saipb.CreateAclEntryRequest, id uint64,
Masks: []byte{byte(req.GetFieldTtl().GetMaskUint())},
})
}
if req.GetFieldRouteDstUserMeta() != nil {
aReq.EntryDesc.GetFlow().Fields = append(aReq.EntryDesc.GetFlow().Fields, &fwdpb.PacketFieldMaskedBytes{
FieldId: &fwdpb.PacketFieldId{Field: &fwdpb.PacketField{FieldNum: fwdpb.PacketFieldNum_PACKET_FIELD_NUM_PACKET_ATTRIBUTE_32, Instance: routeDstMeta}},
Bytes: binary.BigEndian.AppendUint32(nil, uint32(req.GetFieldRouteDstUserMeta().GetDataUint())),
Masks: binary.BigEndian.AppendUint32(nil, uint32(req.GetFieldRouteDstUserMeta().GetMaskUint())),
})
}
if len(req.GetUserDefinedFieldGroupMin()) > 0 {
table := &saipb.AclTableAttribute{}
if err := a.mgr.PopulateAllAttributes(fmt.Sprint(req.GetTableId()), table); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions dataplane/saiserver/acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func TestCreateAclEntry(t *testing.T) {
Data: &saipb.AclFieldData_DataUint{DataUint: 0x01},
Mask: &saipb.AclFieldData_MaskUint{MaskUint: 0xff},
},
FieldRouteDstUserMeta: &saipb.AclFieldData{
Data: &saipb.AclFieldData_DataUint{DataUint: 0x2},
Mask: &saipb.AclFieldData_MaskUint{MaskUint: 0xff},
},
},
want: &fwdpb.TableEntryAddRequest{
ContextId: &fwdpb.ContextId{Id: "foo"},
Expand Down Expand Up @@ -186,6 +190,10 @@ func TestCreateAclEntry(t *testing.T) {
FieldId: &fwdpb.PacketFieldId{Field: &fwdpb.PacketField{FieldNum: fwdpb.PacketFieldNum_PACKET_FIELD_NUM_IP_HOP}},
Bytes: []byte{0x01},
Masks: []byte{0xff},
}, {
FieldId: &fwdpb.PacketFieldId{Field: &fwdpb.PacketField{FieldNum: fwdpb.PacketFieldNum_PACKET_FIELD_NUM_PACKET_ATTRIBUTE_32, Instance: routeDstMeta}},
Bytes: []byte{0x0, 0x0, 0x0, 0x02},
Masks: []byte{0x0, 0x0, 0x0, 0xff},
}},
},
},
Expand Down
8 changes: 8 additions & 0 deletions dataplane/saiserver/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,14 @@ func (l *lag) RemoveLagMember(ctx context.Context, req *saipb.RemoveLagMemberReq
return &saipb.RemoveLagMemberResponse{}, err
}

func (l *lag) SetLagAttribute(context.Context, *saipb.SetLagAttributeRequest) (*saipb.SetLagAttributeResponse, error) {
return &saipb.SetLagAttributeResponse{}, nil
}

func (l *lag) SetLagMemberAttribute(context.Context, *saipb.SetLagMemberAttributeRequest) (*saipb.SetLagMemberAttributeResponse, error) {
return &saipb.SetLagMemberAttributeResponse{}, nil
}

type queue struct {
saipb.UnimplementedQueueServer
mgr *attrmgr.AttrMgr
Expand Down
20 changes: 15 additions & 5 deletions dataplane/saiserver/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ func newRoute(mgr *attrmgr.AttrMgr, dataplane switchDataplaneAPI, s *grpc.Server
return r
}

// routeDstMeta is the key to PACKET_ATTRIBUTE_32 field for routing table metadata.
const routeDstMeta = 0

// CreateRouteEntry creates a new route entry.
func (r *route) CreateRouteEntry(ctx context.Context, req *saipb.CreateRouteEntryRequest) (*saipb.CreateRouteEntryResponse, error) {
fib := FIBV6Table
Expand All @@ -535,21 +538,23 @@ func (r *route) CreateRouteEntry(ctx context.Context, req *saipb.CreateRouteEntr
}
nextType := r.mgr.GetType(fmt.Sprint(req.GetNextHopId()))

actions := []*fwdconfig.ActionBuilder{}

// If the packet action is drop, then next hop is optional.
if forward {
switch nextType {
case saipb.ObjectType_OBJECT_TYPE_NEXT_HOP:
entry.AppendActions(fwdconfig.Action(
actions = append(actions, fwdconfig.Action(
fwdconfig.UpdateAction(fwdpb.UpdateType_UPDATE_TYPE_SET, fwdpb.PacketFieldNum_PACKET_FIELD_NUM_NEXT_HOP_ID).WithUint64Value(req.GetNextHopId())),
fwdconfig.Action(fwdconfig.LookupAction(NHTable)),
)
case saipb.ObjectType_OBJECT_TYPE_NEXT_HOP_GROUP:
entry.AppendActions(fwdconfig.Action(
actions = append(actions, fwdconfig.Action(
fwdconfig.UpdateAction(fwdpb.UpdateType_UPDATE_TYPE_SET, fwdpb.PacketFieldNum_PACKET_FIELD_NUM_NEXT_HOP_GROUP_ID).WithUint64Value(req.GetNextHopId())),
fwdconfig.Action(fwdconfig.LookupAction(NHGTable)),
)
case saipb.ObjectType_OBJECT_TYPE_ROUTER_INTERFACE:
entry.AppendActions(
actions = append(actions,
// Set the next hop IP in the packet's metadata.
fwdconfig.Action(fwdconfig.UpdateAction(fwdpb.UpdateType_UPDATE_TYPE_COPY, fwdpb.PacketFieldNum_PACKET_FIELD_NUM_NEXT_HOP_IP).WithFieldSrc(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_IP_ADDR_DST)),
// Set the output iface.
Expand Down Expand Up @@ -579,7 +584,7 @@ func (r *route) CreateRouteEntry(ctx context.Context, req *saipb.CreateRouteEntr
}
return &saipb.CreateRouteEntryResponse{}, nil
}
entry.AppendActions(
actions = append(actions,
// Set the next hop IP in the packet's metadata.
fwdconfig.Action(fwdconfig.UpdateAction(fwdpb.UpdateType_UPDATE_TYPE_COPY, fwdpb.PacketFieldNum_PACKET_FIELD_NUM_NEXT_HOP_IP).WithFieldSrc(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_IP_ADDR_DST)),
// Set the output port.
Expand All @@ -589,8 +594,13 @@ func (r *route) CreateRouteEntry(ctx context.Context, req *saipb.CreateRouteEntr
return nil, status.Errorf(codes.InvalidArgument, "unknown next hop type: %v", nextType)
}
} else {
entry.AppendActions(fwdconfig.Action(fwdconfig.DropAction()))
actions = append(actions, fwdconfig.Action(fwdconfig.DropAction()))
}
if req.MetaData != nil {
actions = append(actions, fwdconfig.Action(fwdconfig.UpdateAction(fwdpb.UpdateType_UPDATE_TYPE_SET, fwdpb.PacketFieldNum_PACKET_FIELD_NUM_PACKET_ATTRIBUTE_32).
WithFieldIDInstance(routeDstMeta).WithValue(binary.BigEndian.AppendUint32(nil, req.GetMetaData()))))
}
entry.AppendActions(actions...)

route := entry.Build()
_, err := r.dataplane.TableEntryAdd(ctx, route)
Expand Down
14 changes: 14 additions & 0 deletions dataplane/saiserver/routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ func TestCreateRouteEntry(t *testing.T) {
SwitchId: 1,
},
PacketAction: saipb.PacketAction_PACKET_ACTION_TRANSIT.Enum(),
MetaData: proto.Uint32(10),
NextHopId: proto.Uint64(100),
},
wantReq: &fwdpb.TableEntryAddRequest{
Expand All @@ -755,6 +756,19 @@ func TestCreateRouteEntry(t *testing.T) {
PortId: &fwdpb.PortId{ObjectId: &fwdpb.ObjectId{Id: "100"}},
},
},
}, {
ActionType: fwdpb.ActionType_ACTION_TYPE_UPDATE,
Action: &fwdpb.ActionDesc_Update{
Update: &fwdpb.UpdateActionDesc{
Type: fwdpb.UpdateType_UPDATE_TYPE_SET,
Field: &fwdpb.PacketFieldId{Field: &fwdpb.PacketField{}},
FieldId: &fwdpb.PacketFieldId{Field: &fwdpb.PacketField{
FieldNum: fwdpb.PacketFieldNum_PACKET_FIELD_NUM_PACKET_ATTRIBUTE_32,
Instance: routeDstMeta,
}},
Value: []uint8{0x00, 0x00, 0x00, 0x0a},
},
},
}},
EntryDesc: &fwdpb.EntryDesc{
Entry: &fwdpb.EntryDesc_Prefix{
Expand Down

0 comments on commit f63a2c1

Please sign in to comment.