Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial tun term impl #384

Merged
merged 8 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .github/workflows/generatedcode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ jobs:
- name: Run Gazelle
run: bazel run //:gazelle
- name: Check Diff
run: test -z "$(git status --porcelain)"
run: |
if test -n "$(git status --porcelain)"; then
git diff HEAD
exit 1
fi
protos:
runs-on: ubuntu-latest
steps:
Expand All @@ -44,4 +48,8 @@ jobs:
- name: Generate Protos
run: tools/genproto.sh
- name: Check Diff
run: test -z "$(git status --porcelain)"
run: |
if test -n "$(git status --porcelain)"; then
git diff HEAD
exit 1
fi
1 change: 0 additions & 1 deletion dataplane/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ go_library(
"//gnmi/oc",
"//gnmi/reconciler",
"//proto/forwarding",
"@com_github_golang_glog//:glog",
"@com_github_openconfig_gnmi//proto/gnmi",
"@com_github_openconfig_ygnmi//ygnmi",
"@org_golang_google_grpc//:go_default_library",
Expand Down
1 change: 1 addition & 0 deletions dataplane/saiserver/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func getForwardingPipeline() []*fwdpb.ActionDesc {
fwdconfig.Action(fwdconfig.LookupAction(IngressVRFTable)).Build(), // Match interface to VRF.
fwdconfig.Action(fwdconfig.LookupAction(PreIngressActionTable)).Build(), // Run pre-ingress actions.
fwdconfig.Action(fwdconfig.DecapAction(fwdpb.PacketHeaderId_PACKET_HEADER_ID_ETHERNET)).Build(), // Decap L2 header.
fwdconfig.Action(fwdconfig.LookupAction(tunTermTable)).Build(), // Decap the packet if we have a tunnel.
fwdconfig.Action(fwdconfig.LookupAction(IngressActionTable)).Build(), // Run ingress action.
fwdconfig.Action(fwdconfig.LookupAction(FIBSelectorTable)).Build(), // Lookup in FIB.
fwdconfig.Action(fwdconfig.EncapAction(fwdpb.PacketHeaderId_PACKET_HEADER_ID_ETHERNET)).Build(), // Encap L2 header.
Expand Down
37 changes: 33 additions & 4 deletions dataplane/saiserver/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,26 +298,55 @@ func (nh *nextHop) CreateNextHop(ctx context.Context, req *saipb.CreateNextHopRe
fwdconfig.Action(fwdconfig.LookupAction(NHActionTable)).Build(),
}
case saipb.NextHopType_NEXT_HOP_TYPE_TUNNEL_ENCAP:
tunnel := &saipb.GetTunnelAttributeResponse{}
err := nh.mgr.PopulateAttributes(&saipb.GetTunnelAttributeRequest{Oid: req.GetTunnelId(), AttrType: []saipb.TunnelAttr{saipb.TunnelAttr_TUNNEL_ATTR_TYPE}}, tunnel)
if err != nil {
return nil, err
}

headerID := fwdpb.PacketHeaderId_PACKET_HEADER_ID_IP6
if len(req.Ip) == 4 {
headerID = fwdpb.PacketHeaderId_PACKET_HEADER_ID_IP4
}
actions = []*fwdpb.ActionDesc{}

actions = []*fwdpb.ActionDesc{
{
switch tunnel.GetAttr().GetType() {
case saipb.TunnelType_TUNNEL_TYPE_IPINIP:
actions = append(actions, &fwdpb.ActionDesc{
ActionType: fwdpb.ActionType_ACTION_TYPE_ENCAP,
Action: &fwdpb.ActionDesc_Encap{
Encap: &fwdpb.EncapActionDesc{
HeaderId: headerID,
},
},
},
})
case saipb.TunnelType_TUNNEL_TYPE_IPINIP_GRE:
actions = append(actions, &fwdpb.ActionDesc{
ActionType: fwdpb.ActionType_ACTION_TYPE_ENCAP,
Action: &fwdpb.ActionDesc_Encap{
Encap: &fwdpb.EncapActionDesc{
HeaderId: fwdpb.PacketHeaderId_PACKET_HEADER_ID_GRE,
},
},
}, &fwdpb.ActionDesc{
ActionType: fwdpb.ActionType_ACTION_TYPE_ENCAP,
Action: &fwdpb.ActionDesc_Encap{
Encap: &fwdpb.EncapActionDesc{
HeaderId: headerID,
},
},
})
default:
return nil, status.Errorf(codes.InvalidArgument, "unsupported tunnel type: %v", tunnel.GetAttr().GetType())
}

actions = append(actions,
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())
}
Expand Down
7 changes: 4 additions & 3 deletions dataplane/saiserver/routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,12 +473,12 @@ 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),
TunnelId: proto.Uint64(15),
Ip: []byte{127, 0, 0, 1},
},
wantAttr: &saipb.NextHopAttribute{
Type: saipb.NextHopType_NEXT_HOP_TYPE_TUNNEL_ENCAP.Enum(),
TunnelId: proto.Uint64(10),
TunnelId: proto.Uint64(15),
Ip: []byte{127, 0, 0, 1},
},
wantReq: &fwdpb.TableEntryAddRequest{
Expand Down Expand Up @@ -531,7 +531,7 @@ func TestCreateNextHop(t *testing.T) {
},
},
Field: &fwdpb.PacketFieldId{Field: &fwdpb.PacketField{}},
Value: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a},
Value: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f},
},
},
}, {
Expand Down Expand Up @@ -570,6 +570,7 @@ func TestCreateNextHop(t *testing.T) {
t.Run(tt.desc, func(t *testing.T) {
dplane := &fakeSwitchDataplane{}
c, mgr, stopFn := newTestNextHop(t, dplane)
mgr.StoreAttributes(15, &saipb.TunnelAttribute{Type: saipb.TunnelType_TUNNEL_TYPE_IPINIP.Enum()})
defer stopFn()
_, gotErr := c.CreateNextHop(context.TODO(), tt.req)
if diff := errdiff.Check(gotErr, tt.wantErr); diff != "" {
Expand Down
17 changes: 17 additions & 0 deletions dataplane/saiserver/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const (
MyMacTable = "my-mac-table"
hostifToPortTable = "cpu-input"
portToHostifTable = "cpu-output"
tunTermTable = "tun-term"
)

func newSwitch(mgr *attrmgr.AttrMgr, engine switchDataplaneAPI, s *grpc.Server, opts *dplaneopts.Options) (*saiSwitch, error) {
Expand Down Expand Up @@ -537,6 +538,22 @@ func (sw *saiSwitch) CreateSwitch(ctx context.Context, _ *saipb.CreateSwitchRequ
if err != nil {
return nil, err
}
_, err = sw.dataplane.TableCreate(ctx, &fwdpb.TableCreateRequest{
ContextId: &fwdpb.ContextId{Id: sw.dataplane.ID()},
Desc: &fwdpb.TableDesc{
Actions: []*fwdpb.ActionDesc{{ActionType: fwdpb.ActionType_ACTION_TYPE_CONTINUE}},
TableType: fwdpb.TableType_TABLE_TYPE_FLOW,
TableId: &fwdpb.TableId{ObjectId: &fwdpb.ObjectId{Id: tunTermTable}},
Table: &fwdpb.TableDesc_Flow{
Flow: &fwdpb.FlowTableDesc{
BankCount: 1,
},
},
},
})
if err != nil {
return nil, err
}

cpuPortID, err := sw.port.createCPUPort(ctx)
if err != nil {
Expand Down
124 changes: 124 additions & 0 deletions dataplane/saiserver/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,127 @@ func (t *tunnel) CreateTunnel(ctx context.Context, req *saipb.CreateTunnelReques
Oid: id,
}, nil
}

var (
ipV4ExactMask = []byte{0xFF, 0xFF, 0xFF, 0xFF}
ipV4AnyMask = make([]byte, 4)
ipV6ExactMask = []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
ipV6AnyMask = make([]byte, 16)
)

func (t *tunnel) CreateTunnelTermTableEntry(ctx context.Context, req *saipb.CreateTunnelTermTableEntryRequest) (*saipb.CreateTunnelTermTableEntryResponse, error) {
id := t.mgr.NextID()

fields := []*fwdpb.PacketFieldMaskedBytes{}

// It is valid for some request fields to be omitted, so initialize slices to the correct length for the given IP protocol.
isV4 := len(req.SrcIp) == 4 || len(req.DstIp) == 4
zeroIP := ipV6AnyMask
exactMask := ipV6ExactMask
headerID := fwdpb.PacketHeaderId_PACKET_HEADER_ID_IP6
if isV4 {
zeroIP = ipV4AnyMask
exactMask = ipV4ExactMask
headerID = fwdpb.PacketHeaderId_PACKET_HEADER_ID_IP4
}

srcIP := req.GetSrcIp()
if req.SrcIp == nil {
srcIP = zeroIP
}

dstIP := req.GetDstIp()
if req.DstIp == nil {
dstIP = zeroIP
}

srcIPMask := req.GetSrcIpMask()
if req.SrcIpMask == nil {
srcIPMask = zeroIP
}

dstIPMask := req.GetDstIpMask()
if req.DstIpMask == nil {
dstIPMask = zeroIP
}

switch req.GetType() {
case saipb.TunnelTermTableEntryType_TUNNEL_TERM_TABLE_ENTRY_TYPE_P2P: // src IP, dst IP
fields = append(fields,
fwdconfig.PacketFieldMaskedBytes(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_IP_ADDR_SRC).WithBytes(srcIP, exactMask).Build(),
fwdconfig.PacketFieldMaskedBytes(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_IP_ADDR_DST).WithBytes(dstIP, exactMask).Build(),
)
case saipb.TunnelTermTableEntryType_TUNNEL_TERM_TABLE_ENTRY_TYPE_P2MP: // src IP, dst IP & mask
fields = append(fields,
fwdconfig.PacketFieldMaskedBytes(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_IP_ADDR_SRC).WithBytes(srcIP, exactMask).Build(),
fwdconfig.PacketFieldMaskedBytes(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_IP_ADDR_DST).WithBytes(dstIP, dstIPMask).Build(),
)
case saipb.TunnelTermTableEntryType_TUNNEL_TERM_TABLE_ENTRY_TYPE_MP2P: // src IP & mask, dst IP
fields = append(fields,
fwdconfig.PacketFieldMaskedBytes(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_IP_ADDR_SRC).WithBytes(srcIP, srcIPMask).Build(),
fwdconfig.PacketFieldMaskedBytes(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_IP_ADDR_DST).WithBytes(dstIP, exactMask).Build(),
)
case saipb.TunnelTermTableEntryType_TUNNEL_TERM_TABLE_ENTRY_TYPE_MP2MP: // src IP & mask, dst IP &mask
fields = append(fields,
fwdconfig.PacketFieldMaskedBytes(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_IP_ADDR_SRC).WithBytes(srcIP, srcIPMask).Build(),
fwdconfig.PacketFieldMaskedBytes(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_IP_ADDR_DST).WithBytes(srcIP, dstIPMask).Build(),
)
default:
return nil, status.Errorf(codes.InvalidArgument, "invalid tunnel type: %v", req.GetType())
}

var actions []*fwdpb.ActionDesc
switch req.GetTunnelType() { // TODO: The tunnel type should be used to match packets as well.
case saipb.TunnelType_TUNNEL_TYPE_IPINIP:
actions = append(actions, &fwdpb.ActionDesc{
ActionType: fwdpb.ActionType_ACTION_TYPE_DECAP,
Action: &fwdpb.ActionDesc_Decap{
Decap: &fwdpb.DecapActionDesc{
HeaderId: headerID,
},
},
})
case saipb.TunnelType_TUNNEL_TYPE_IPINIP_GRE:
actions = append(actions, &fwdpb.ActionDesc{
ActionType: fwdpb.ActionType_ACTION_TYPE_DECAP,
Action: &fwdpb.ActionDesc_Decap{
Decap: &fwdpb.DecapActionDesc{
HeaderId: headerID,
},
},
}, &fwdpb.ActionDesc{
ActionType: fwdpb.ActionType_ACTION_TYPE_DECAP,
Action: &fwdpb.ActionDesc_Decap{
Decap: &fwdpb.DecapActionDesc{
HeaderId: fwdpb.PacketHeaderId_PACKET_HEADER_ID_GRE,
},
},
})
default:
return nil, status.Errorf(codes.InvalidArgument, "invalid tunnel type: %v", req.GetType())
}
actions = append(actions,
fwdconfig.Action(fwdconfig.UpdateAction(fwdpb.UpdateType_UPDATE_TYPE_SET, fwdpb.PacketFieldNum_PACKET_FIELD_NUM_PACKET_VRF).WithUint64Value(req.GetVrId())).Build(),
)

tReq := &fwdpb.TableEntryAddRequest{
ContextId: &fwdpb.ContextId{Id: t.dataplane.ID()},
TableId: &fwdpb.TableId{ObjectId: &fwdpb.ObjectId{Id: tunTermTable}},
EntryDesc: &fwdpb.EntryDesc{Entry: &fwdpb.EntryDesc_Flow{
Flow: &fwdpb.FlowEntryDesc{
Id: uint32(id),
Priority: 1,
Bank: 0,
Fields: fields,
},
}},
Actions: actions,
}
if _, err := t.dataplane.TableEntryAdd(ctx, tReq); err != nil {
return nil, err
}

return &saipb.CreateTunnelTermTableEntryResponse{
Oid: id,
}, nil
}
Loading