Skip to content

Commit

Permalink
Add vlan remote mechanism support
Browse files Browse the repository at this point in the history
Add domain pool mapping

See related:
networkservicemesh/cmd-forwarder-vpp#365
networkservicemesh/sdk-kernel#355
networkservicemesh/api#112

related api change in commit a84c763d06096835432c313dde19b3b3e25c3d2f

Signed-off-by: Laszlo Kiraly <laszlo.kiraly@est.tech>
  • Loading branch information
ljkiraly committed Nov 11, 2021
1 parent 0eea333 commit 78dcb45
Show file tree
Hide file tree
Showing 14 changed files with 1,020 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/networkservicemesh/api v1.0.1-0.20211110183123-3038992da61a
github.com/networkservicemesh/sdk v0.5.1-0.20211110200317-2272de7cade7
github.com/networkservicemesh/sdk-kernel v0.0.0-20211110200529-c70dbe94eb13
github.com/networkservicemesh/sdk-sriov v0.0.0-20211014093500-f12ea1fa1fb9
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
github.com/thanhpk/randstr v1.0.4
Expand Down
7 changes: 6 additions & 1 deletion pkg/networkservice/chains/forwarder/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"net"
"net/url"
"sync"
"time"

"git.fd.io/govpp.git/api"
Expand All @@ -50,6 +51,8 @@ import (
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/connectioncontext/mtu"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/kernel"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/memif"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/vlan"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/vlan/pciaddresspool"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/vxlan"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/wireguard"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/pinhole"
Expand All @@ -70,13 +73,14 @@ type xconnectNSServer struct {
}

// NewServer - returns an implementation of the xconnectns network service
func NewServer(ctx context.Context, name string, authzServer networkservice.NetworkServiceServer, tokenGenerator token.GeneratorFunc, clientURL *url.URL, vppConn Connection, tunnelIP net.IP, tunnelPort uint16, dialTimeout time.Duration, clientDialOptions ...grpc.DialOption) endpoint.Endpoint {
func NewServer(ctx context.Context, name string, authzServer networkservice.NetworkServiceServer, tokenGenerator token.GeneratorFunc, clientURL *url.URL, vppConn Connection, tunnelIP net.IP, tunnelPort uint16, dialTimeout time.Duration, resourcePool pciaddresspool.PCIAddressPool, clientDialOptions ...grpc.DialOption) endpoint.Endpoint {
nseClient := registryclient.NewNetworkServiceEndpointRegistryClient(ctx, clientURL,
registryclient.WithNSEAdditionalFunctionality(registryrecvfd.NewNetworkServiceEndpointRegistryClient()),
registryclient.WithDialOptions(clientDialOptions...),
)
nsClient := registryclient.NewNetworkServiceRegistryClient(ctx, clientURL, registryclient.WithDialOptions(clientDialOptions...))

resourceLock := &sync.Mutex{}
rv := &xconnectNSServer{}
additionalFunctionality := []networkservice.NetworkServiceServer{
recvfd.NewServer(),
Expand Down Expand Up @@ -114,6 +118,7 @@ func NewServer(ctx context.Context, name string, authzServer networkservice.Netw
kernel.NewClient(vppConn),
vxlan.NewClient(vppConn, tunnelIP, vxlan.WithVniPort(tunnelPort)),
wireguard.NewClient(vppConn, tunnelIP),
vlan.NewClient(vppConn, resourceLock, resourcePool),
filtermechanisms.NewClient(),
pinhole.NewClient(vppConn),
recvfd.NewClient(),
Expand Down
189 changes: 189 additions & 0 deletions pkg/networkservice/mechanisms/vlan/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// Copyright (c) 2021 Nordix Foundation.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package vlan

import (
"context"
"io"
"sync"
"time"

"git.fd.io/govpp.git/api"
interfaces "github.com/edwarnicke/govpp/binapi/interface"
"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"google.golang.org/grpc"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/cls"
vlanmech "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/vlan"
"github.com/networkservicemesh/api/pkg/api/networkservice/payload"
kernellink "github.com/networkservicemesh/sdk-kernel/pkg/kernel"
"github.com/networkservicemesh/sdk-kernel/pkg/kernel/tools/nshandle"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/postpone"

"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/vlan/l2vtr"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/vlan/linkinit"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/vlan/pciaddresspool"
"github.com/networkservicemesh/sdk-vpp/pkg/tools/ifindex"
)

type vlanClient struct {
vppConn api.Connection
}

// NewClient returns a VLAN client chain element
func NewClient(vppConn api.Connection, resourceLock sync.Locker, resourcePool pciaddresspool.PCIAddressPool) networkservice.NetworkServiceClient {
return chain.NewNetworkServiceClient(
l2vtr.NewClient(vppConn),
&vlanClient{vppConn: vppConn},
linkinit.NewClient(vppConn),
pciaddresspool.NewClient(resourceLock, resourcePool),
)
}

func (v *vlanClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
if request.GetConnection().GetPayload() != payload.Ethernet {
return next.Client(ctx).Request(ctx, request, opts...)
}

mechanism := &networkservice.Mechanism{
Cls: cls.REMOTE,
Type: vlanmech.MECHANISM,
Parameters: make(map[string]string),
}
request.MechanismPreferences = append(request.MechanismPreferences, mechanism)

postponeCtxFunc := postpone.ContextWithValues(ctx)

conn, err := next.Client(ctx).Request(ctx, request, opts...)
if err != nil {
return nil, err
}

if err := addSubIf(ctx, conn, v.vppConn); err != nil {
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()

if _, closeErr := v.Close(closeCtx, conn, opts...); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}

return nil, err
}

return conn, nil
}

func (v *vlanClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
if conn.GetPayload() != payload.Ethernet {
return next.Client(ctx).Close(ctx, conn, opts...)
}
_ = delSubIf(ctx, conn, v.vppConn)
return next.Client(ctx).Close(ctx, conn, opts...)
}

func addSubIf(ctx context.Context, conn *networkservice.Connection, vppConn api.Connection) error {
if mechanism := vlanmech.ToMechanism(conn.GetMechanism()); mechanism != nil {
_, ok := ifindex.Load(ctx, true)
if ok {
return nil
}
now := time.Now()
pciAddressInfo, ok := pciaddresspool.Load(ctx)
if !ok {
return errors.Errorf("no link found")
}
hostNetNS, err := nshandle.Current()
if err != nil {
return err
}
defer func() { _ = hostNetNS.Close() }()
kernelLink, err := kernellink.FindHostDevice(pciAddressInfo.GetPCIAddress(), "", hostNetNS)
if err != nil {
return err
}
hostIFName := kernelLink.GetName()

client, err := interfaces.NewServiceClient(vppConn).SwInterfaceDump(ctx, &interfaces.SwInterfaceDump{
NameFilterValid: true,
NameFilter: hostIFName,
})
if err != nil {
return errors.Wrapf(err, "error attempting to get interface dump client to set vlan subinterface on %q", hostIFName)
}
log.FromContext(ctx).
WithField("duration", time.Since(now)).
WithField("HostInterfaceName", hostIFName).
WithField("vppapi", "SwInterfaceDump").Debug("completed")

for {
details, err := client.Recv()
if err == io.EOF {
break
}
if err != nil {
return errors.Wrapf(err, "error attempting to get interface details to set vlan subinterface on %q", hostIFName)
}
now = time.Now()
swIfIndex := details.SwIfIndex
vlanID := mechanism.GetVlanID()
vlanSubif := &interfaces.CreateVlanSubif{
SwIfIndex: swIfIndex,
VlanID: vlanID,
}

rsp, err := interfaces.NewServiceClient(vppConn).CreateVlanSubif(ctx, vlanSubif)
if err != nil {
return errors.WithStack(err)
}
log.FromContext(ctx).
WithField("duration", time.Since(now)).
WithField("HostInterfaceIndex", swIfIndex).
WithField("VlanID", vlanID).
WithField("vppapi", "CreateVlanSubIf").Debug("completed")

ifindex.Store(ctx, true, rsp.SwIfIndex)
}
}
return nil
}
func delSubIf(ctx context.Context, conn *networkservice.Connection, vppConn api.Connection) error {
if mechanism := vlanmech.ToMechanism(conn.GetMechanism()); mechanism != nil {
swIfIndex, ok := ifindex.Load(ctx, true)
if !ok {
return nil
}
now := time.Now()
vlanSubif := &interfaces.DeleteSubif{
SwIfIndex: swIfIndex,
}
_, err := interfaces.NewServiceClient(vppConn).DeleteSubif(ctx, vlanSubif)
if err != nil {
return errors.WithStack(err)
}
log.FromContext(ctx).
WithField("duration", time.Since(now)).
WithField("HostInterfaceIndex", swIfIndex).
WithField("vppapi", "DeleteSubif").Debug("completed")
ifindex.Delete(ctx, true)
}
return nil
}
18 changes: 18 additions & 0 deletions pkg/networkservice/mechanisms/vlan/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2021 Nordix Foundation.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package vlan provides chain elements for implementing the vlan mechanism
package vlan
121 changes: 121 additions & 0 deletions pkg/networkservice/mechanisms/vlan/l2vtr/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright (c) 2021 Nordix Foundation.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package l2vtr

import (
"context"
"time"

"git.fd.io/govpp.git/api"
"github.com/edwarnicke/govpp/binapi/l2"
"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"google.golang.org/grpc"

"github.com/networkservicemesh/api/pkg/api/networkservice"
vlanmech "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/vlan"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/postpone"

"github.com/networkservicemesh/sdk-vpp/pkg/tools/ifindex"
)

type l2vtrClient struct {
vppConn api.Connection
}

func NewClient(vppConn api.Connection) networkservice.NetworkServiceClient {
return &l2vtrClient{vppConn: vppConn}
}

func (v *l2vtrClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
postponeCtxFunc := postpone.ContextWithValues(ctx)

conn, err := next.Client(ctx).Request(ctx, request, opts...)
if err != nil {
return nil, err
}

if err = enableVtr(ctx, conn, v.vppConn); err != nil {
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()

if _, closeErr := v.Close(closeCtx, conn, opts...); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}

return nil, err
}

return conn, nil
}

func (v *l2vtrClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
_ = disableVtr(ctx, conn, v.vppConn)
return next.Client(ctx).Close(ctx, conn, opts...)
}

func enableVtr(ctx context.Context, conn *networkservice.Connection, vppConn api.Connection) error {
if mechanism := vlanmech.ToMechanism(conn.GetMechanism()); mechanism != nil {
swIfIndex, ok := ifindex.Load(ctx, true)
if !ok {
return nil
}
now := time.Now()
if _, err := l2.NewServiceClient(vppConn).L2InterfaceVlanTagRewrite(ctx, &l2.L2InterfaceVlanTagRewrite{
SwIfIndex: swIfIndex,
VtrOp: L2_VTR_POP_1,
PushDot1q: 0,
Tag1: 0,
Tag2: 0,
}); err != nil {
return errors.WithStack(err)
}
log.FromContext(ctx).
WithField("duration", time.Since(now)).
WithField("SwIfIndex", swIfIndex).
WithField("operation", "POP 1").
WithField("vppapi", "L2InterfaceVlanTagRewrite").Debug("completed")

}
return nil
}

func disableVtr(ctx context.Context, conn *networkservice.Connection, vppConn api.Connection) error {
if mechanism := vlanmech.ToMechanism(conn.GetMechanism()); mechanism != nil {
swIfIndex, ok := ifindex.Load(ctx, true)
if !ok {
return nil
}
now := time.Now()

if _, err := l2.NewServiceClient(vppConn).L2InterfaceVlanTagRewrite(ctx, &l2.L2InterfaceVlanTagRewrite{
SwIfIndex: swIfIndex,
VtrOp: L2_VTR_DISABLED,
}); err != nil {
return errors.WithStack(err)
}
log.FromContext(ctx).
WithField("duration", time.Since(now)).
WithField("SwIfIndex", swIfIndex).
WithField("operation", "DISABLE").
WithField("vppapi", "L2InterfaceVlanTagRewrite").Debug("completed")

}
return nil
}
Loading

0 comments on commit 78dcb45

Please sign in to comment.