Skip to content

Commit

Permalink
Make vxlan port configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Zoltan Lugossy <zoltan.lugossy@est.tech>
  • Loading branch information
zolug committed Sep 29, 2021
1 parent dd436eb commit b312ac9
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 8 deletions.
17 changes: 13 additions & 4 deletions pkg/networkservice/common/mechanisms/vxlan/vni/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,22 @@ import (
)

type vniClient struct {
tunnelIP net.IP
tunnelIP net.IP
tunnelPort uint16
}

// NewClient - set the SrcIP for the vxlan mechanism
func NewClient(tunnelIP net.IP) networkservice.NetworkServiceClient {
func NewClient(tunnelIP net.IP, options ...Option) networkservice.NetworkServiceClient {
opts := &vniOpions{
tunnelPort: vxlanPort,
}
for _, opt := range options {
opt(opts)
}

return &vniClient{
tunnelIP: tunnelIP,
tunnelIP: tunnelIP,
tunnelPort: opts.tunnelPort,
}
}

Expand All @@ -47,7 +56,7 @@ func (v *vniClient) Request(ctx context.Context, request *networkservice.Network
// Note: This only has effect if this is a vxlan mechanism
if mech := vxlan.ToMechanism(m); mech != nil {
mech.SetSrcIP(v.tunnelIP)
mech.SetSrcPort(vxlanPort)
mech.SetSrcPort(v.tunnelPort)
}
}
return next.Client(ctx).Request(ctx, request, opts...)
Expand Down
12 changes: 12 additions & 0 deletions pkg/networkservice/common/mechanisms/vxlan/vni/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,23 @@ func TestVNIClient(t *testing.T) {
},
},
}
var port uint16 = 0
c := next.NewNetworkServiceClient(vni.NewClient(net.ParseIP("192.0.2.1")))
conn, err := c.Request(context.Background(), request)
assert.Nil(t, err)
assert.NotNil(t, conn)
mechanism := vxlan.ToMechanism(request.MechanismPreferences[0])
assert.NotNil(t, mechanism)
assert.Equal(t, "192.0.2.1", mechanism.SrcIP().String())
assert.NotEqual(t, port, mechanism.SrcPort())

port = 4466
c = next.NewNetworkServiceClient(vni.NewClient(net.ParseIP("192.0.2.1"), vni.WithTunnelPort(port)))
conn, err = c.Request(context.Background(), request)
assert.Nil(t, err)
assert.NotNil(t, conn)
mechanism = vxlan.ToMechanism(request.MechanismPreferences[0])
assert.NotNil(t, mechanism)
assert.Equal(t, "192.0.2.1", mechanism.SrcIP().String())
assert.Equal(t, port, mechanism.SrcPort())
}
35 changes: 35 additions & 0 deletions pkg/networkservice/common/mechanisms/vxlan/vni/option.go
Original file line number Diff line number Diff line change
@@ -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 vni

// Option is an option pattern for vni server/client
type Option func(o *vniOpions)

// WithTunnelPort sets VxLAN port
func WithTunnelPort(tunnelPort uint16) Option {
return func(o *vniOpions) {
if tunnelPort != 0 {
o.tunnelPort = tunnelPort
}
}
}

type vniOpions struct {
tunnelPort uint16
}
17 changes: 13 additions & 4 deletions pkg/networkservice/common/mechanisms/vxlan/vni/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,23 @@ import (
)

type vniServer struct {
tunnelIP net.IP
tunnelIP net.IP
tunnelPort uint16
sync.Map
}

// NewServer - set the DstIP *and* VNI for the vxlan mechanism
func NewServer(tunnelIP net.IP) networkservice.NetworkServiceServer {
func NewServer(tunnelIP net.IP, options ...Option) networkservice.NetworkServiceServer {
opts := &vniOpions{
tunnelPort: vxlanPort,
}
for _, opt := range options {
opt(opts)
}

return &vniServer{
tunnelIP: tunnelIP,
tunnelIP: tunnelIP,
tunnelPort: opts.tunnelPort,
}
}

Expand All @@ -49,7 +58,7 @@ func (v *vniServer) Request(ctx context.Context, request *networkservice.Network
return next.Server(ctx).Request(ctx, request)
}
mechanism.SetDstIP(v.tunnelIP)
mechanism.SetDstPort(vxlanPort)
mechanism.SetDstPort(v.tunnelPort)
k := vniKey{
srcIPString: mechanism.SrcIP().String(),
vni: mechanism.VNI(),
Expand Down
13 changes: 13 additions & 0 deletions pkg/networkservice/common/mechanisms/vxlan/vni/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,26 @@ func TestVNIServer(t *testing.T) {
Type: vxlan.MECHANISM,
}
vxlan.ToMechanism(request.GetConnection().GetMechanism()).SetSrcIP(net.ParseIP("192.0.2.1"))
var port uint16 = 0
server := next.NewNetworkServiceServer(vni.NewServer(net.ParseIP("192.0.2.2")))
conn, err := server.Request(context.Background(), request)
assert.Nil(t, err)
assert.NotNil(t, conn)
mechanism := vxlan.ToMechanism(conn.GetMechanism())
assert.NotNil(t, mechanism)
assert.Equal(t, "192.0.2.2", mechanism.DstIP().String())
assert.NotEqual(t, port, mechanism.DstPort())
assert.NotEqual(t, 0, mechanism.VNI())

port = 4466
server = next.NewNetworkServiceServer(vni.NewServer(net.ParseIP("192.0.2.2"), vni.WithTunnelPort(port)))
conn, err = server.Request(context.Background(), request)
assert.Nil(t, err)
assert.NotNil(t, conn)
mechanism = vxlan.ToMechanism(conn.GetMechanism())
assert.NotNil(t, mechanism)
assert.Equal(t, "192.0.2.2", mechanism.DstIP().String())
assert.Equal(t, port, mechanism.DstPort())
assert.NotEqual(t, 0, mechanism.VNI())
}

Expand Down

0 comments on commit b312ac9

Please sign in to comment.