generated from networkservicemesh/cmd-template
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added kernel to vxlan to kernel test
Signed-off-by: Mikhail Avramenko <avramenkomihail15@gmail.com>
- Loading branch information
1 parent
c06c5d8
commit 15fbbdd
Showing
4 changed files
with
338 additions
and
1,392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2020 Doc.ai and/or its affiliates. | ||
// | ||
// 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 copyfile provides the necessary mechanisms to request and inject a kernel interface. | ||
package copyfile | ||
|
||
import ( | ||
"context" | ||
"github.com/golang/protobuf/ptypes/empty" | ||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next" | ||
"net/url" | ||
) | ||
|
||
type copyFileServer struct{ | ||
name string | ||
} | ||
|
||
// NewServer - creates a NetworkServiceServer that requests a kernel interface and populates the netns inode | ||
func NewServer(name string) networkservice.NetworkServiceServer { | ||
return ©FileServer{ | ||
name: name, | ||
} | ||
} | ||
|
||
func (m *copyFileServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) { | ||
if mechanism := kernel.ToMechanism(request.GetConnection().GetMechanism()); mechanism != nil { | ||
//nsHandle, err := mechutils.ToNSHandle(mechanism) | ||
//fd, err := syscall.Open("/run/netns/" + m.name, unix.O_RDONLY|unix.O_CLOEXEC, 0) | ||
//if err != nil { | ||
// return nil, err | ||
//} | ||
//filename := fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), fd) | ||
mechanism.SetNetNSURL((&url.URL{Scheme: "file", Path: "/run/netns/" + m.name}).String()) | ||
} | ||
return next.Server(ctx).Request(ctx, request) | ||
} | ||
|
||
func (m *copyFileServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) { | ||
return next.Server(ctx).Close(ctx, conn) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package tests | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"git.fd.io/govpp.git/api" | ||
"github.com/edwarnicke/vpphelper" | ||
"github.com/google/uuid" | ||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/cls" | ||
"github.com/networkservicemesh/cmd-forwarder-vpp/internal/tests/copyfile" | ||
"github.com/networkservicemesh/cmd-forwarder-vpp/internal/tests/ns" | ||
"github.com/networkservicemesh/sdk-kernel/pkg/kernel/networkservice/connectioncontextkernel" | ||
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/connectioncontext" | ||
"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/vxlan" | ||
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/wireguard" | ||
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/pinhole" | ||
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/stats" | ||
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/tag" | ||
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/up" | ||
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/xconnect" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/chains/endpoint" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/authorize" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms" | ||
kernelmechanism "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/kernel" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanismtranslation" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/refresh" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/serialize" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/updatepath" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/adapters" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/ipam/point2pointipam" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" | ||
"github.com/networkservicemesh/sdk/pkg/tools/token" | ||
"github.com/thanhpk/randstr" | ||
"github.com/vishvananda/netns" | ||
"net" | ||
) | ||
|
||
type kernelToVxlanVerifiableEndpoint struct { | ||
ctx context.Context | ||
vppConn api.Connection | ||
endpointNSHandle netns.NsHandle | ||
endpoint.Endpoint | ||
} | ||
|
||
func newKernelToVxlanVerifiableEndpoint(ctx context.Context, | ||
prefix1, prefix2 *net.IPNet, | ||
tokenGenerator token.GeneratorFunc, | ||
vppConn vpphelper.Connection) verifiableEndpoint { | ||
|
||
rootNSHandle, err := netns.Get() | ||
if err != nil { | ||
panic(fmt.Sprintf("unable to get root netNs: %+v", err)) | ||
} | ||
endpointNSName := fmt.Sprintf("endpoint-%s", randstr.Hex(4)) | ||
endpointNSHandle, err := netns.NewNamed(endpointNSName) | ||
if err != nil { | ||
panic(fmt.Sprintf("unable create netNs %s: %+v", endpointNSName, err)) | ||
} | ||
go func(endpointNsName string) { | ||
<-ctx.Done() | ||
_ = netns.DeleteNamed(endpointNsName) | ||
}(endpointNSName) | ||
|
||
rv := &kernelToVxlanVerifiableEndpoint{ | ||
ctx: ctx, | ||
vppConn: vppConn, | ||
endpointNSHandle: endpointNSHandle, | ||
} | ||
name := "vxlanVerifiableEndpoint" | ||
rv.Endpoint = endpoint.NewServer(ctx, | ||
tokenGenerator, | ||
endpoint.WithName(name), | ||
endpoint.WithAuthorizeServer(authorize.NewServer()), | ||
endpoint.WithAdditionalFunctionality( | ||
metadata.NewServer(), | ||
connectioncontext.NewServer(vppConn), | ||
up.NewServer(ctx, vppConn), | ||
xconnect.NewServer(vppConn), | ||
connectioncontextkernel.NewServer(), | ||
tag.NewServer(ctx, vppConn), | ||
mtu.NewServer(vppConn), | ||
pinhole.NewServer(vppConn), | ||
mechanisms.NewServer(map[string]networkservice.NetworkServiceServer{ | ||
vxlan.MECHANISM: vxlan.NewServer(vppConn, net.ParseIP(serverIP)), | ||
}), | ||
adapters.NewClientToServer(clientChain(ctx, vppConn, net.ParseIP(serverIP))), | ||
updatepath.NewServer("ep-" + uuid.New().String()), | ||
//metadata.NewServer(), | ||
point2pointipam.NewServer(prefix1), | ||
point2pointipam.NewServer(prefix2), | ||
mechanisms.NewServer(map[string]networkservice.NetworkServiceServer{ | ||
kernel.MECHANISM: chain.NewNetworkServiceServer( | ||
kernelmechanism.NewServer(), | ||
), | ||
}), | ||
ns.NewServer(endpointNSHandle), | ||
copyfile.NewServer(endpointNSName), | ||
ns.NewServer(rootNSHandle), | ||
), | ||
) | ||
|
||
return rv | ||
} | ||
|
||
func clientChain(ctx context.Context, vppConn vpphelper.Connection, tunnelIP net.IP) networkservice.NetworkServiceClient{ | ||
return chain.NewNetworkServiceClient( | ||
[]networkservice.NetworkServiceClient{ | ||
mechanismtranslation.NewClient(), | ||
updatepath.NewClient("client-" + uuid.New().String()), | ||
serialize.NewClient(), | ||
refresh.NewClient(ctx), | ||
metadata.NewClient(), | ||
mechanismtranslation.NewClient(), | ||
connectioncontextkernel.NewClient(), | ||
stats.NewClient(ctx), | ||
up.NewClient(ctx, vppConn), | ||
mtu.NewClient(vppConn), | ||
tag.NewClient(ctx, vppConn), | ||
// mechanisms | ||
memif.NewClient(vppConn), | ||
kernel.NewClient(vppConn), | ||
vxlan.NewClient(vppConn, tunnelIP), | ||
wireguard.NewClient(vppConn, tunnelIP), | ||
pinhole.NewClient(vppConn), | ||
}...) | ||
} | ||
|
||
func (v *kernelToVxlanVerifiableEndpoint) VerifyConnection(conn *networkservice.Connection) error { | ||
//namingConn := conn.Clone() | ||
//namingConn.Id = conn.GetPath().GetPathSegments()[len(conn.GetPath().GetPathSegments())-1].GetId() | ||
//namingConn.Mechanism = &networkservice.Mechanism{ | ||
// Cls: cls.LOCAL, | ||
// Type: kernel.MECHANISM, | ||
// Parameters: map[string]string{ | ||
// krnl.InterfaceNameKey : "ns-", | ||
// }, | ||
//} | ||
////rootNSHandle, err := netns.Get() | ||
////if err != nil { | ||
//// panic(fmt.Sprintf("unable to get root netNs: %+v", err)) | ||
////} | ||
//if err := checkKernelInterface(namingConn, conn.GetContext().GetIpContext().GetDstIPNets(), v.endpointNSHandle); err != nil { | ||
// return err | ||
//} | ||
namingConn := conn.Clone() | ||
namingConn.Id = conn.GetPath().GetPathSegments()[len(conn.GetPath().GetPathSegments())-1].GetId() | ||
namingConn.Mechanism = &networkservice.Mechanism{ | ||
Cls: cls.LOCAL, | ||
Type: kernel.MECHANISM, | ||
} | ||
if err := checkKernelInterface(namingConn, conn.GetContext().GetIpContext().GetDstIPNets(), v.endpointNSHandle); err != nil { | ||
return err | ||
} | ||
for _, ip := range conn.GetContext().GetIpContext().GetSrcIPNets() { | ||
if err := pingKernel(ip, v.endpointNSHandle); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (v *kernelToVxlanVerifiableEndpoint) VerifyClose(conn *networkservice.Connection) error { | ||
return checkNoKernelInterface(conn, v.endpointNSHandle) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.