From 58e4c417123c36c632e332dd3d5a90d98a0d8c33 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 | 7 ++-- pkg/networkservice/mechanisms/vxlan/client.go | 9 ++++- pkg/networkservice/mechanisms/vxlan/common.go | 9 ++--- .../mechanisms/vxlan/constants.go | 3 +- pkg/networkservice/mechanisms/vxlan/option.go | 37 +++++++++++++++++++ pkg/networkservice/mechanisms/vxlan/server.go | 9 ++++- 6 files changed, 60 insertions(+), 14 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..c7b7bd77 100644 --- a/pkg/networkservice/chains/xconnectns/server.go +++ b/pkg/networkservice/chains/xconnectns/server.go @@ -37,6 +37,7 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms" "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/recvfd" "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/sendfd" + "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/vxlan/vni" "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanismtranslation" "github.com/networkservicemesh/sdk/pkg/networkservice/core/adapters" "github.com/networkservicemesh/sdk/pkg/tools/addressof" @@ -67,7 +68,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 +87,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.WithVniOptions(vni.WithTunnelPort(tunnelPort))), wireguard.MECHANISM: wireguard.NewServer(vppConn, tunnelIP), }), pinhole.NewServer(vppConn), @@ -104,7 +105,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.WithVniOptions(vni.WithTunnelPort(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..1ad38e23 100644 --- a/pkg/networkservice/mechanisms/vxlan/client.go +++ b/pkg/networkservice/mechanisms/vxlan/client.go @@ -44,13 +44,18 @@ 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 := &vxlanOpions{} + for _, opt := range options { + opt(opts) + } + return chain.NewNetworkServiceClient( &vxlanClient{ vppConn: vppConn, }, mtu.NewClient(vppConn, tunnelIP), - vni.NewClient(tunnelIP), + vni.NewClient(tunnelIP, opts.vniOptions...), ) } 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/constants.go b/pkg/networkservice/mechanisms/vxlan/constants.go index 9ed20d72..b4e2000e 100644 --- a/pkg/networkservice/mechanisms/vxlan/constants.go +++ b/pkg/networkservice/mechanisms/vxlan/constants.go @@ -22,6 +22,5 @@ import ( const ( // MECHANISM string - MECHANISM = vxlan.MECHANISM - vxlanDefaultPort = 4789 + MECHANISM = vxlan.MECHANISM ) diff --git a/pkg/networkservice/mechanisms/vxlan/option.go b/pkg/networkservice/mechanisms/vxlan/option.go new file mode 100644 index 00000000..bc4152a8 --- /dev/null +++ b/pkg/networkservice/mechanisms/vxlan/option.go @@ -0,0 +1,37 @@ +// 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 + +import ( + "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/vxlan/vni" +) + +// Option is an option pattern for vxlan server/client +type Option func(o *vxlanOpions) + +// WithVniOptions sets vni options +func WithVniOptions(vniOptions ...vni.Option) Option { + return func(o *vxlanOpions) { + o.vniOptions = vniOptions + } +} + +type vxlanOpions struct { + vniOptions []vni.Option +} diff --git a/pkg/networkservice/mechanisms/vxlan/server.go b/pkg/networkservice/mechanisms/vxlan/server.go index eb3304e2..6ad757ec 100644 --- a/pkg/networkservice/mechanisms/vxlan/server.go +++ b/pkg/networkservice/mechanisms/vxlan/server.go @@ -42,9 +42,14 @@ 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 := &vxlanOpions{} + for _, opt := range options { + opt(opts) + } + return chain.NewNetworkServiceServer( - vni.NewServer(tunnelIP), + vni.NewServer(tunnelIP, opts.vniOptions...), mtu.NewServer(vppConn, tunnelIP), &vxlanServer{ vppConn: vppConn,