Skip to content

Commit

Permalink
tunnel
Browse files Browse the repository at this point in the history
  • Loading branch information
DanG100 committed Feb 21, 2024
1 parent 9b2733d commit 25f0f6a
Show file tree
Hide file tree
Showing 12 changed files with 361 additions and 55 deletions.
24 changes: 20 additions & 4 deletions dataplane/forwarding/fwdconfig/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ func (ab *ActionBuilder) Build() *fwdpb.ActionDesc {

// UpdateActionBuilder is a builder for an update action.
type UpdateActionBuilder struct {
fieldIDNum fwdpb.PacketFieldNum
updateType fwdpb.UpdateType
fieldSrc fwdpb.PacketFieldNum
value []byte
fieldIDNum fwdpb.PacketFieldNum
updateType fwdpb.UpdateType
fieldSrc fwdpb.PacketFieldNum
instance uint32
srcInstance uint32
value []byte
}

// UpdateAction returns a new update action builder.
Expand All @@ -91,6 +93,18 @@ func (u *UpdateActionBuilder) WithUpdateType(t fwdpb.UpdateType) *UpdateActionBu
return u
}

// WithFieldIDInstance sets the instance of the id num.
func (u *UpdateActionBuilder) WithFieldIDInstance(i uint32) *UpdateActionBuilder {
u.instance = i
return u
}

// WithFieldSrcInstance sets the instance of the src id num.
func (u *UpdateActionBuilder) WithFieldSrcInstance(i uint32) *UpdateActionBuilder {
u.srcInstance = i
return u
}

// WithUpdateType sets the update source field id.
func (u *UpdateActionBuilder) WithFieldSrc(num fwdpb.PacketFieldNum) *UpdateActionBuilder {
u.fieldSrc = num
Expand All @@ -115,13 +129,15 @@ func (u *UpdateActionBuilder) set(ad *fwdpb.ActionDesc) {
FieldId: &fwdpb.PacketFieldId{
Field: &fwdpb.PacketField{
FieldNum: u.fieldIDNum,
Instance: u.instance,
},
},
Type: u.updateType,
Value: u.value,
Field: &fwdpb.PacketFieldId{
Field: &fwdpb.PacketField{
FieldNum: u.fieldSrc,
Instance: u.srcInstance,
},
},
},
Expand Down
14 changes: 8 additions & 6 deletions dataplane/forwarding/fwdconfig/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ func (b TableEntryAddRequestBuilder) Build() *fwdpb.TableEntryAddRequest {
Entries: []*fwdpb.TableEntryAddRequest_Entry{},
}
for i, entry := range b.entries {
var acts []*fwdpb.ActionDesc
entry := &fwdpb.TableEntryAddRequest_Entry{
EntryDesc: entry.Build(),
}
req.Entries = append(req.Entries, entry)
if i >= len(b.actions) {
break
}
for _, act := range b.actions[i] {
acts = append(acts, act.Build())
entry.Actions = append(entry.Actions, act.Build())
}
req.Entries = append(req.Entries, &fwdpb.TableEntryAddRequest_Entry{
EntryDesc: entry.Build(),
Actions: acts,
})
}
return req
}
Expand Down
4 changes: 4 additions & 0 deletions dataplane/forwarding/protocol/attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ var FieldAttr = map[fwdpb.PacketFieldNum]struct {
fwdpb.PacketFieldNum_PACKET_FIELD_NUM_OUTPUT_IFACE: {
Sizes: []int{SizeUint64},
},
fwdpb.PacketFieldNum_PACKET_FIELD_NUM_TUNNEL_ID: {
Sizes: []int{SizeUint64},
},
}

// GroupAttr contains attributes for each packet header group.
Expand Down Expand Up @@ -221,6 +224,7 @@ var GroupAttr = map[fwdpb.PacketHeaderGroup]struct {
fwdpb.PacketFieldNum_PACKET_FIELD_NUM_TRAP_ID,
fwdpb.PacketFieldNum_PACKET_FIELD_NUM_INPUT_IFACE,
fwdpb.PacketFieldNum_PACKET_FIELD_NUM_OUTPUT_IFACE,
fwdpb.PacketFieldNum_PACKET_FIELD_NUM_TUNNEL_ID,
},
},
fwdpb.PacketHeaderGroup_PACKET_HEADER_GROUP_L2: {
Expand Down
6 changes: 6 additions & 0 deletions dataplane/forwarding/protocol/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Metadata struct {
trapID []byte // ID of the trap rule that was applies to this packet.
inputIface []byte // L3 input interface id.
outputIface []byte // L3 output interface id.
tunnelID []byte // Tunnel ID
desc *protocol.Desc // Protocol descriptor.
}

Expand Down Expand Up @@ -120,6 +121,8 @@ func (m *Metadata) Field(id fwdpacket.FieldID) ([]byte, error) {
return m.outputIface, nil
case fwdpb.PacketFieldNum_PACKET_FIELD_NUM_TRAP_ID:
return m.trapID, nil
case fwdpb.PacketFieldNum_PACKET_FIELD_NUM_TUNNEL_ID:
return m.tunnelID, nil

default:
return nil, fmt.Errorf("metadata: Field %v failed, unsupported field", id)
Expand Down Expand Up @@ -220,6 +223,9 @@ func (m *Metadata) updateSet(id fwdpacket.FieldID, arg []byte) (bool, error) {
case fwdpb.PacketFieldNum_PACKET_FIELD_NUM_OUTPUT_IFACE:
m.outputIface = arg
return true, nil
case fwdpb.PacketFieldNum_PACKET_FIELD_NUM_TUNNEL_ID:
m.tunnelID = arg
return true, nil
default:
return false, fmt.Errorf("metadata: UpdateField failed, set unsupported for field %v", id)
}
Expand Down
2 changes: 2 additions & 0 deletions dataplane/saiserver/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ go_library(
"routing.go",
"saiserver.go",
"switch.go",
"tunnel.go",
],
importpath = "github.com/openconfig/lemming/dataplane/saiserver",
visibility = ["//visibility:public"],
Expand Down Expand Up @@ -39,6 +40,7 @@ go_test(
"ports_test.go",
"routing_test.go",
"switch_test.go",
"tunnel_test.go",
],
embed = [":saiserver"],
deps = [
Expand Down
48 changes: 26 additions & 22 deletions dataplane/saiserver/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,37 +230,41 @@ func newNextHop(mgr *attrmgr.AttrMgr, dataplane switchDataplaneAPI, s *grpc.Serv
func (nh *nextHop) CreateNextHop(ctx context.Context, req *saipb.CreateNextHopRequest) (*saipb.CreateNextHopResponse, error) {
id := nh.mgr.NextID()

if req.GetType() != saipb.NextHopType_NEXT_HOP_TYPE_IP {
return nil, status.Errorf(codes.InvalidArgument, "unsupported req type: %v", req.GetType())
}
var actions []*fwdpb.ActionDesc

switch req.GetType() {
case saipb.NextHopType_NEXT_HOP_TYPE_IP:
actions = []*fwdpb.ActionDesc{
fwdconfig.Action(fwdconfig.UpdateAction(fwdpb.UpdateType_UPDATE_TYPE_SET, fwdpb.PacketFieldNum_PACKET_FIELD_NUM_OUTPUT_IFACE).WithUint64Value(req.GetRouterInterfaceId())).Build(),
fwdconfig.Action(fwdconfig.UpdateAction(fwdpb.UpdateType_UPDATE_TYPE_SET, fwdpb.PacketFieldNum_PACKET_FIELD_NUM_NEXT_HOP_IP).WithValue(req.GetIp())).Build(),
fwdconfig.Action(fwdconfig.LookupAction(NHActionTable)).Build(),
}
case saipb.NextHopType_NEXT_HOP_TYPE_TUNNEL_ENCAP:
ip := req.GetIp()
tunnel := req.GetTunnelId()
mac := req.GetTunnelMac()
}

actions := []*fwdpb.ActionDesc{
{
ActionType: fwdpb.ActionType_ACTION_TYPE_ENCAP,
Action: &fwdpb.ActionDesc_Encap{
Encap: &fwdpb.EncapActionDesc{
HeaderId: fwdpb.PacketHeaderId_PACKET_HEADER_ID_IP,
actions = []*fwdpb.ActionDesc{
{
ActionType: fwdpb.ActionType_ACTION_TYPE_ENCAP,
Action: &fwdpb.ActionDesc_Encap{
Encap: &fwdpb.EncapActionDesc{
HeaderId: fwdpb.PacketHeaderId_PACKET_HEADER_ID_IP,
},
},
},
},
fwdconfig.Action(fwdconfig.UpdateAction(fwdpb.UpdateType_UPDATE_TYPE_SET, fwdpb.PacketFieldNum_PACKET_FIELD_NUM_IP_ADDR_DST).WithValue(req.GetIp())).Build(),
fwdconfig.Action(fwdconfig.UpdateAction(fwdpb.UpdateType_UPDATE_TYPE_SET, fwdpb.PacketFieldNum_PACKET_FIELD_NUM_IP_ADDR_DST).WithValue(req.GetIp())).Build(),
fwdconfig.Action(fwdconfig.UpdateAction(fwdpb.UpdateType_UPDATE_TYPE_SET, fwdpb.PacketFieldNum_PACKET_FIELD_NUM_NEXT_HOP_IP).WithValue(req.GetIp())).Build(),
fwdconfig.Action(fwdconfig.UpdateAction(fwdpb.UpdateType_UPDATE_TYPE_SET, fwdpb.PacketFieldNum_PACKET_FIELD_NUM_TUNNEL_ID).WithUint64Value(req.GetTunnelId())).Build(),
fwdconfig.Action(fwdconfig.LookupAction(NHActionTable)).Build(),
fwdconfig.Action(fwdconfig.LookupAction(TunnelEncap)).Build(),
}
default:
return nil, status.Errorf(codes.InvalidArgument, "unsupported req type: %v", req.GetType())
}
// SRC IP == encap tunnel IP or tunnel interface ip???

nhReq := fwdconfig.TableEntryAddRequest(nh.dataplane.ID(), NHTable).AppendEntry(
fwdconfig.EntryDesc(fwdconfig.ExactEntry(fwdconfig.PacketFieldBytes(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_NEXT_HOP_ID).WithUint64(id))),
fwdconfig.Action(fwdconfig.UpdateAction(fwdpb.UpdateType_UPDATE_TYPE_SET, fwdpb.PacketFieldNum_PACKET_FIELD_NUM_OUTPUT_IFACE).WithUint64Value(req.GetRouterInterfaceId())),
fwdconfig.Action(fwdconfig.UpdateAction(fwdpb.UpdateType_UPDATE_TYPE_SET, fwdpb.PacketFieldNum_PACKET_FIELD_NUM_NEXT_HOP_IP).WithValue(req.GetIp())),
fwdconfig.Action(fwdconfig.LookupAction(NHActionTable)),
)
).Build()
nhReq.Entries[0].Actions = actions

if _, err := nh.dataplane.TableEntryAdd(ctx, nhReq.Build()); err != nil {
if _, err := nh.dataplane.TableEntryAdd(ctx, nhReq); err != nil {
return nil, err
}
return &saipb.CreateNextHopResponse{
Expand Down
98 changes: 97 additions & 1 deletion dataplane/saiserver/routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func TestCreateNextHop(t *testing.T) {
req: &saipb.CreateNextHopRequest{},
wantErr: "InvalidArgument",
}, {
desc: "success",
desc: "success ip next hop",
req: &saipb.CreateNextHopRequest{
Type: saipb.NextHopType_NEXT_HOP_TYPE_IP.Enum(),
RouterInterfaceId: proto.Uint64(10),
Expand Down Expand Up @@ -296,6 +296,102 @@ func TestCreateNextHop(t *testing.T) {
},
}},
},
}, {
desc: "success tunnel next hop",
req: &saipb.CreateNextHopRequest{
Type: saipb.NextHopType_NEXT_HOP_TYPE_TUNNEL_ENCAP.Enum(),
TunnelId: proto.Uint64(10),
Ip: []byte{127, 0, 0, 1},
},
wantAttr: &saipb.NextHopAttribute{
Type: saipb.NextHopType_NEXT_HOP_TYPE_TUNNEL_ENCAP.Enum(),
TunnelId: proto.Uint64(10),
Ip: []byte{127, 0, 0, 1},
},
wantReq: &fwdpb.TableEntryAddRequest{
ContextId: &fwdpb.ContextId{Id: "foo"},
TableId: &fwdpb.TableId{ObjectId: &fwdpb.ObjectId{Id: NHTable}},
Entries: []*fwdpb.TableEntryAddRequest_Entry{{
Actions: []*fwdpb.ActionDesc{{
ActionType: fwdpb.ActionType_ACTION_TYPE_ENCAP,
Action: &fwdpb.ActionDesc_Encap{
Encap: &fwdpb.EncapActionDesc{
HeaderId: fwdpb.PacketHeaderId_PACKET_HEADER_ID_IP,
},
},
}, {
ActionType: fwdpb.ActionType_ACTION_TYPE_UPDATE,
Action: &fwdpb.ActionDesc_Update{
Update: &fwdpb.UpdateActionDesc{
Type: fwdpb.UpdateType_UPDATE_TYPE_SET,
FieldId: &fwdpb.PacketFieldId{
Field: &fwdpb.PacketField{
FieldNum: fwdpb.PacketFieldNum_PACKET_FIELD_NUM_IP_ADDR_DST,
},
},
Field: &fwdpb.PacketFieldId{Field: &fwdpb.PacketField{}},
Value: []byte{0x7f, 0x00, 0x00, 0x01},
},
},
}, {
ActionType: fwdpb.ActionType_ACTION_TYPE_UPDATE,
Action: &fwdpb.ActionDesc_Update{
Update: &fwdpb.UpdateActionDesc{
Type: fwdpb.UpdateType_UPDATE_TYPE_SET,
FieldId: &fwdpb.PacketFieldId{
Field: &fwdpb.PacketField{
FieldNum: fwdpb.PacketFieldNum_PACKET_FIELD_NUM_NEXT_HOP_IP,
},
},
Field: &fwdpb.PacketFieldId{Field: &fwdpb.PacketField{}},
Value: []byte{0x7f, 0x00, 0x00, 0x01},
},
},
}, {
ActionType: fwdpb.ActionType_ACTION_TYPE_UPDATE,
Action: &fwdpb.ActionDesc_Update{
Update: &fwdpb.UpdateActionDesc{
Type: fwdpb.UpdateType_UPDATE_TYPE_SET,
FieldId: &fwdpb.PacketFieldId{
Field: &fwdpb.PacketField{
FieldNum: fwdpb.PacketFieldNum_PACKET_FIELD_NUM_TUNNEL_ID,
},
},
Field: &fwdpb.PacketFieldId{Field: &fwdpb.PacketField{}},
Value: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a},
},
},
}, {
ActionType: fwdpb.ActionType_ACTION_TYPE_LOOKUP,
Action: &fwdpb.ActionDesc_Lookup{
Lookup: &fwdpb.LookupActionDesc{
TableId: &fwdpb.TableId{ObjectId: &fwdpb.ObjectId{Id: NHActionTable}},
},
},
}, {
ActionType: fwdpb.ActionType_ACTION_TYPE_LOOKUP,
Action: &fwdpb.ActionDesc_Lookup{
Lookup: &fwdpb.LookupActionDesc{
TableId: &fwdpb.TableId{ObjectId: &fwdpb.ObjectId{Id: TunnelEncap}},
},
},
}},
EntryDesc: &fwdpb.EntryDesc{
Entry: &fwdpb.EntryDesc_Exact{
Exact: &fwdpb.ExactEntryDesc{
Fields: []*fwdpb.PacketFieldBytes{{
Bytes: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
FieldId: &fwdpb.PacketFieldId{
Field: &fwdpb.PacketField{
FieldNum: fwdpb.PacketFieldNum_PACKET_FIELD_NUM_NEXT_HOP_ID,
},
},
}},
},
},
},
}},
},
}}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions dataplane/saiserver/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const (
IngressActionTable = "ingress-table"
EgressActionTable = "egress-action-table"
NHActionTable = "nh-action"
TunnelEncap = "tunnel-encap"
)

func newSwitch(mgr *attrmgr.AttrMgr, engine switchDataplaneAPI, s *grpc.Server, opts *dplaneopts.Options) (*saiSwitch, error) {
Expand Down
Loading

0 comments on commit 25f0f6a

Please sign in to comment.