From 80cc6e351bfaaf49aaabfd76fe793d977128901b Mon Sep 17 00:00:00 2001 From: Zoltan Lugossy Date: Tue, 28 Sep 2021 20:48:17 +0200 Subject: [PATCH] Make vxlan port configurable networkservicemesh/sdk commit message: Make vxlan port configurable (see https://github.com/networkservicemesh/sdk/pull/1091) Signed-off-by: Zoltan Lugossy --- .../chains/xconnectns/server.go | 6 ++-- pkg/networkservice/mechanisms/vxlan/client.go | 11 ++++-- pkg/networkservice/mechanisms/vxlan/common.go | 9 +++-- pkg/networkservice/mechanisms/vxlan/option.go | 35 +++++++++++++++++++ pkg/networkservice/mechanisms/vxlan/server.go | 11 ++++-- 5 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 pkg/networkservice/mechanisms/vxlan/option.go diff --git a/pkg/networkservice/chains/xconnectns/server.go b/pkg/networkservice/chains/xconnectns/server.go index 0f25195b..6630174d 100644 --- a/pkg/networkservice/chains/xconnectns/server.go +++ b/pkg/networkservice/chains/xconnectns/server.go @@ -67,7 +67,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, 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, clientDialOptions ...grpc.DialOption) endpoint.Endpoint { rv := &xconnectNSServer{} additionalFunctionality := []networkservice.NetworkServiceServer{ recvfd.NewServer(), @@ -86,7 +86,7 @@ func NewServer(ctx context.Context, name string, authzServer networkservice.Netw mechanisms.NewServer(map[string]networkservice.NetworkServiceServer{ memif.MECHANISM: memif.NewServer(vppConn, memif.WithDirectMemif()), kernel.MECHANISM: kernel.NewServer(vppConn), - vxlan.MECHANISM: vxlan.NewServer(vppConn, tunnelIP), + vxlan.MECHANISM: vxlan.NewServer(vppConn, tunnelIP, vxlan.WithVniPort(tunnelPort)), wireguard.MECHANISM: wireguard.NewServer(vppConn, tunnelIP), }), pinhole.NewServer(vppConn), @@ -104,7 +104,7 @@ func NewServer(ctx context.Context, name string, authzServer networkservice.Netw // mechanisms memif.NewClient(vppConn), kernel.NewClient(vppConn), - vxlan.NewClient(vppConn, tunnelIP), + vxlan.NewClient(vppConn, tunnelIP, vxlan.WithVniPort(tunnelPort)), wireguard.NewClient(vppConn, tunnelIP), pinhole.NewClient(vppConn), recvfd.NewClient(), diff --git a/pkg/networkservice/mechanisms/vxlan/client.go b/pkg/networkservice/mechanisms/vxlan/client.go index c5b060ec..451560a7 100644 --- a/pkg/networkservice/mechanisms/vxlan/client.go +++ b/pkg/networkservice/mechanisms/vxlan/client.go @@ -44,13 +44,20 @@ type vxlanClient struct { } // NewClient - returns a new client for the vxlan remote mechanism -func NewClient(vppConn api.Connection, tunnelIP net.IP) networkservice.NetworkServiceClient { +func NewClient(vppConn api.Connection, tunnelIP net.IP, options ...Option) networkservice.NetworkServiceClient { + opts := &vxlanOptions{ + vxlanPort: vxlanDefaultPort, + } + for _, opt := range options { + opt(opts) + } + return chain.NewNetworkServiceClient( &vxlanClient{ vppConn: vppConn, }, mtu.NewClient(vppConn, tunnelIP), - vni.NewClient(tunnelIP), + vni.NewClient(tunnelIP, vni.WithTunnelPort(opts.vxlanPort)), ) } diff --git a/pkg/networkservice/mechanisms/vxlan/common.go b/pkg/networkservice/mechanisms/vxlan/common.go index 032eff09..887ad2f0 100644 --- a/pkg/networkservice/mechanisms/vxlan/common.go +++ b/pkg/networkservice/mechanisms/vxlan/common.go @@ -39,9 +39,6 @@ func addDel(ctx context.Context, conn *networkservice.Connection, vppConn api.Co if isClient { port = mechanism.SrcPort() } - if port != vxlanDefaultPort { - return errors.Errorf("vxlan only supports port %d not port %d", vxlanDefaultPort, port) - } _, ok := ifindex.Load(ctx, isClient) if isAdd && ok { return nil @@ -75,19 +72,21 @@ func addDel(ctx context.Context, conn *networkservice.Connection, vppConn api.Co WithField("vppapi", "AddNodeNext").Debug("completed") now = time.Now() - vxlanAddDelTunnel := &vxlan.VxlanAddDelTunnel{ + vxlanAddDelTunnel := &vxlan.VxlanAddDelTunnelV2{ IsAdd: isAdd, Instance: ^uint32(0), SrcAddress: types.ToVppAddress(mechanism.SrcIP()), DstAddress: types.ToVppAddress(mechanism.DstIP()), DecapNextIndex: addNextNodeRsp.NextIndex, Vni: mechanism.VNI(), + SrcPort: port, + DstPort: port, } if !isClient { vxlanAddDelTunnel.SrcAddress = types.ToVppAddress(mechanism.DstIP()) vxlanAddDelTunnel.DstAddress = types.ToVppAddress(mechanism.SrcIP()) } - rsp, err := vxlan.NewServiceClient(vppConn).VxlanAddDelTunnel(ctx, vxlanAddDelTunnel) + rsp, err := vxlan.NewServiceClient(vppConn).VxlanAddDelTunnelV2(ctx, vxlanAddDelTunnel) if err != nil { return errors.WithStack(err) } diff --git a/pkg/networkservice/mechanisms/vxlan/option.go b/pkg/networkservice/mechanisms/vxlan/option.go new file mode 100644 index 00000000..78a958a5 --- /dev/null +++ b/pkg/networkservice/mechanisms/vxlan/option.go @@ -0,0 +1,35 @@ +// Copyright (c) 2020-2021 Cisco and/or its affiliates. +// +// 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 vxlan + +// Option is an option pattern for vxlan server/client +type Option func(o *vxlanOptions) + +// WithVniPort sets vni port +func WithVniPort(port uint16) Option { + return func(o *vxlanOptions) { + if port != 0 { + o.vxlanPort = port + } + } +} + +type vxlanOptions struct { + vxlanPort uint16 +} diff --git a/pkg/networkservice/mechanisms/vxlan/server.go b/pkg/networkservice/mechanisms/vxlan/server.go index eb3304e2..b43d4f94 100644 --- a/pkg/networkservice/mechanisms/vxlan/server.go +++ b/pkg/networkservice/mechanisms/vxlan/server.go @@ -42,9 +42,16 @@ type vxlanServer struct { } // NewServer - returns a new server for the vxlan remote mechanism -func NewServer(vppConn api.Connection, tunnelIP net.IP) networkservice.NetworkServiceServer { +func NewServer(vppConn api.Connection, tunnelIP net.IP, options ...Option) networkservice.NetworkServiceServer { + opts := &vxlanOptions{ + vxlanPort: vxlanDefaultPort, + } + for _, opt := range options { + opt(opts) + } + return chain.NewNetworkServiceServer( - vni.NewServer(tunnelIP), + vni.NewServer(tunnelIP, vni.WithTunnelPort(opts.vxlanPort)), mtu.NewServer(vppConn, tunnelIP), &vxlanServer{ vppConn: vppConn,