Skip to content

Commit

Permalink
Add vlan remote mechanism support
Browse files Browse the repository at this point in the history
See related:
networkservicemesh/cmd-forwarder-vpp#365
networkservicemesh/api#112
networkservicemesh/sdk-kernel#355

Signed-off-by: Laszlo Kiraly <laszlo.kiraly@est.tech>
  • Loading branch information
ljkiraly committed Nov 15, 2021
1 parent 410164c commit 8c0f323
Show file tree
Hide file tree
Showing 13 changed files with 722 additions and 4 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ require (
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20200609130330-bd2cb7843e1b
google.golang.org/grpc v1.35.0
google.golang.org/protobuf v1.25.0
gopkg.in/yaml.v2 v2.2.4 // indirect
)
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,8 @@ gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMy
gopkg.in/square/go-jose.v2 v2.4.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
6 changes: 5 additions & 1 deletion pkg/networkservice/chains/forwarder/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/networkservicemesh/sdk/pkg/tools/token"

"github.com/networkservicemesh/sdk-kernel/pkg/kernel/networkservice/connectioncontextkernel"
"github.com/networkservicemesh/sdk-kernel/pkg/kernel/networkservice/ethernetcontext"

"github.com/networkservicemesh/sdk/pkg/networkservice/common/discover"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/roundrobin"
Expand All @@ -50,6 +51,7 @@ 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/vxlan"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/wireguard"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/pinhole"
Expand All @@ -70,7 +72,7 @@ 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, domain2Device map[string]string, clientDialOptions ...grpc.DialOption) endpoint.Endpoint {
nseClient := registryclient.NewNetworkServiceEndpointRegistryClient(ctx, clientURL,
registryclient.WithNSEAdditionalFunctionality(registryrecvfd.NewNetworkServiceEndpointRegistryClient()),
registryclient.WithDialOptions(clientDialOptions...),
Expand All @@ -87,6 +89,7 @@ func NewServer(ctx context.Context, name string, authzServer networkservice.Netw
up.NewServer(ctx, vppConn),
xconnect.NewServer(vppConn),
connectioncontextkernel.NewServer(),
ethernetcontext.NewVFServer(),
tag.NewServer(ctx, vppConn),
mtu.NewServer(vppConn),
mechanisms.NewServer(map[string]networkservice.NetworkServiceServer{
Expand Down Expand Up @@ -114,6 +117,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, domain2Device),
filtermechanisms.NewClient(),
pinhole.NewClient(vppConn),
recvfd.NewClient(),
Expand Down
186 changes: 186 additions & 0 deletions pkg/networkservice/mechanisms/vlan/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// 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"
"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"
"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/hwaddress"
"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/tools/ifindex"
)

const (
serviceDomainLabel = "serviceDomain"
)

type vlanClient struct {
vppConn api.Connection
deviceNames map[string]string
}

// NewClient returns a VLAN client chain element
func NewClient(vppConn api.Connection, domain2Device map[string]string) networkservice.NetworkServiceClient {
return chain.NewNetworkServiceClient(
hwaddress.NewClient(vppConn),
l2vtr.NewClient(vppConn),
&vlanClient{
vppConn: vppConn,
deviceNames: domain2Device,
},
linkinit.NewClient(vppConn, domain2Device),
)
}

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, v.deviceNames); 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, deviceNames map[string]string) error {
if mechanism := vlanmech.ToMechanism(conn.GetMechanism()); mechanism != nil {
_, ok := ifindex.Load(ctx, true)
if ok {
return nil
}
now := time.Now()
serviceDomain := conn.GetLabels()[serviceDomainLabel]
hostIFName, ok := deviceNames[serviceDomain]
if !ok {
return errors.Errorf("no interface name for service domain %s", serviceDomain)
}

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
72 changes: 72 additions & 0 deletions pkg/networkservice/mechanisms/vlan/hwaddress/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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 hwaddress

import (
"context"

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

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
"github.com/networkservicemesh/sdk/pkg/tools/postpone"
)

type hwaddressClient struct {
vppConn api.Connection
}

// NewClient - updates ethernet context with hw address
func NewClient(vppConn api.Connection) networkservice.NetworkServiceClient {
return &hwaddressClient{
vppConn: vppConn,
}
}

func (h *hwaddressClient) 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 := setEthContextHwaddress(ctx, conn, h.vppConn, metadata.IsClient(h)); err != nil {
if closeErr := h.closeOnFailure(postponeCtxFunc, conn, opts); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}
return nil, err
}
return conn, nil
}

func (h *hwaddressClient) closeOnFailure(postponeCtxFunc func() (context.Context, context.CancelFunc), conn *networkservice.Connection, opts []grpc.CallOption) error {
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()

_, err := h.Close(closeCtx, conn, opts...)

return err
}

func (h *hwaddressClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
return next.Client(ctx).Close(ctx, conn, opts...)
}
61 changes: 61 additions & 0 deletions pkg/networkservice/mechanisms/vlan/hwaddress/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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 hwaddress

import (
"context"
"time"

"git.fd.io/govpp.git/api"
"github.com/pkg/errors"

interfaces "github.com/edwarnicke/govpp/binapi/interface"
"github.com/networkservicemesh/api/pkg/api/networkservice"
vlanmech "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/vlan"
"github.com/networkservicemesh/sdk/pkg/tools/log"

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

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

rsp, err := interfaces.NewServiceClient(vppConn).SwInterfaceGetMacAddress(ctx, &interfaces.SwInterfaceGetMacAddress{
SwIfIndex: swIfIndex})
if err != nil {
return errors.WithStack(err)
}
log.FromContext(ctx).
WithField("duration", time.Since(now)).
WithField("HostInterfaceIndex", swIfIndex).
WithField("HwAddress", rsp.MacAddress).
WithField("vppapi", "SwInterfaceGetMacAddress").Debug("completed")

if conn.GetContext().GetEthernetContext() == nil {
conn.GetContext().EthernetContext = new(networkservice.EthernetContext)
}
ethernetContext := conn.GetContext().GetEthernetContext()

ethernetContext.SrcMac = rsp.MacAddress.String()
}
return nil
}
Loading

0 comments on commit 8c0f323

Please sign in to comment.