diff --git a/.golangci.yml b/.golangci.yml index 41b8e3bb..6a6f7a25 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -187,3 +187,7 @@ issues: linters: - funlen text: "Function 'addDel'" + - path: pkg/networkservice/chains/forwarder/server.go + linters: + - funlen + text: "Function 'NewServer'" diff --git a/go.mod b/go.mod index 7d2330fd..4618f5ba 100644 --- a/go.mod +++ b/go.mod @@ -7,9 +7,10 @@ require ( github.com/edwarnicke/govpp v0.0.0-20220311182453-f32f292e0e91 github.com/edwarnicke/serialize v1.0.7 github.com/golang/protobuf v1.5.2 + github.com/google/uuid v1.1.2 github.com/hashicorp/go-multierror v1.1.1 github.com/networkservicemesh/api v1.2.1-0.20220315001249-f33f8c3f2feb - github.com/networkservicemesh/sdk v0.5.1-0.20220316101237-288caa7bbc1c + github.com/networkservicemesh/sdk v0.5.1-0.20220316105041-b88289b9022e github.com/networkservicemesh/sdk-kernel v0.0.0-20220316101641-0103343013f0 github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.7.0 diff --git a/go.sum b/go.sum index 1f5ffe86..20849515 100644 --- a/go.sum +++ b/go.sum @@ -188,8 +188,9 @@ github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OS github.com/nats-io/stan.go v0.10.2/go.mod h1:vo2ax8K2IxaR3JtEMLZRFKIdoK/3o1/PKueapB7ezX0= github.com/networkservicemesh/api v1.2.1-0.20220315001249-f33f8c3f2feb h1:5047nbh591kIjLNlFrO0NbpRVkOtCGRSvkKKjQyEDyI= github.com/networkservicemesh/api v1.2.1-0.20220315001249-f33f8c3f2feb/go.mod h1:B6meq/SWjWR6bGXZdXPfbOeaBK+T1JayLdtEJQCsXKU= -github.com/networkservicemesh/sdk v0.5.1-0.20220316101237-288caa7bbc1c h1:R0DVEMRYMg5SAuPQ48R3lTUD5Dpu+epJfrsmbDMY8mw= github.com/networkservicemesh/sdk v0.5.1-0.20220316101237-288caa7bbc1c/go.mod h1:fK/G5mfEDIefFXxRSNXqj1jjd8SEUeZMpQUdCuc7owY= +github.com/networkservicemesh/sdk v0.5.1-0.20220316105041-b88289b9022e h1:IXzvAfoU6tCTEZxAGfJGbKjVfome2Iymm1vkiM9MhZ8= +github.com/networkservicemesh/sdk v0.5.1-0.20220316105041-b88289b9022e/go.mod h1:fK/G5mfEDIefFXxRSNXqj1jjd8SEUeZMpQUdCuc7owY= github.com/networkservicemesh/sdk-kernel v0.0.0-20220316101641-0103343013f0 h1:PpT01M2DzAt6vBLbmjbTn3u7vrzHUuLoE6WbUy9pk2M= github.com/networkservicemesh/sdk-kernel v0.0.0-20220316101641-0103343013f0/go.mod h1:nbwaNH+kCji5C8R0RxdZHqAPTNXvX7St/NUJ+bwmbIs= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= diff --git a/pkg/networkservice/chains/forwarder/options.go b/pkg/networkservice/chains/forwarder/options.go new file mode 100644 index 00000000..f1bbd175 --- /dev/null +++ b/pkg/networkservice/chains/forwarder/options.go @@ -0,0 +1,113 @@ +// Copyright (c) 2022 Cisco 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. + +// +build linux + +package forwarder + +import ( + "net/url" + "time" + + "google.golang.org/grpc" + + "github.com/networkservicemesh/api/pkg/api/networkservice" + "github.com/networkservicemesh/sdk/pkg/networkservice/common/cleanup" + + "github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/vxlan" + "github.com/networkservicemesh/sdk-vpp/pkg/networkservice/stats" +) + +type forwarderOptions struct { + name string + authorizeServer networkservice.NetworkServiceServer + clientURL *url.URL + dialTimeout time.Duration + domain2Device map[string]string + statsOpts []stats.Option + cleanupOpts []cleanup.Option + vxlanOpts []vxlan.Option + dialOpts []grpc.DialOption +} + +// Option is an option pattern for forwarder chain elements +type Option func(o *forwarderOptions) + +// WithName - set a forwarder name +func WithName(name string) Option { + return func(o *forwarderOptions) { + o.name = name + } +} + +// WithAuthorizeServer sets authorization server chain element +func WithAuthorizeServer(authorizeServer networkservice.NetworkServiceServer) Option { + if authorizeServer == nil { + panic("Authorize server cannot be nil") + } + return func(o *forwarderOptions) { + o.authorizeServer = authorizeServer + } +} + +// WithClientURL sets clientURL. +func WithClientURL(clientURL *url.URL) Option { + return func(c *forwarderOptions) { + c.clientURL = clientURL + } +} + +// WithDialTimeout sets dial timeout for the client +func WithDialTimeout(dialTimeout time.Duration) Option { + return func(o *forwarderOptions) { + o.dialTimeout = dialTimeout + } +} + +// WithVlanDomain2Device sets vlan option +func WithVlanDomain2Device(domain2Device map[string]string) Option { + return func(o *forwarderOptions) { + o.domain2Device = domain2Device + } +} + +// WithStatsOptions sets stats options +func WithStatsOptions(opts ...stats.Option) Option { + return func(o *forwarderOptions) { + o.statsOpts = opts + } +} + +// WithCleanupOptions sets cleanup options +func WithCleanupOptions(opts ...cleanup.Option) Option { + return func(o *forwarderOptions) { + o.cleanupOpts = opts + } +} + +// WithVxlanOptions sets vxlan options +func WithVxlanOptions(opts ...vxlan.Option) Option { + return func(o *forwarderOptions) { + o.vxlanOpts = opts + } +} + +// WithDialOptions sets dial options +func WithDialOptions(opts ...grpc.DialOption) Option { + return func(o *forwarderOptions) { + o.dialOpts = opts + } +} diff --git a/pkg/networkservice/chains/forwarder/server.go b/pkg/networkservice/chains/forwarder/server.go index eafa5221..71f7c7fe 100644 --- a/pkg/networkservice/chains/forwarder/server.go +++ b/pkg/networkservice/chains/forwarder/server.go @@ -27,28 +27,30 @@ import ( "time" "git.fd.io/govpp.git/api" - "github.com/networkservicemesh/sdk/pkg/networkservice/chains/client" - "github.com/networkservicemesh/sdk/pkg/networkservice/common/connect" - "github.com/networkservicemesh/sdk/pkg/networkservice/common/filtermechanisms" - "google.golang.org/grpc" + "github.com/google/uuid" "github.com/networkservicemesh/api/pkg/api/networkservice" + "github.com/networkservicemesh/sdk/pkg/networkservice/chains/client" "github.com/networkservicemesh/sdk/pkg/networkservice/chains/endpoint" + "github.com/networkservicemesh/sdk/pkg/networkservice/common/authorize" + "github.com/networkservicemesh/sdk/pkg/networkservice/common/cleanup" + "github.com/networkservicemesh/sdk/pkg/networkservice/common/connect" + "github.com/networkservicemesh/sdk/pkg/networkservice/common/discover" + "github.com/networkservicemesh/sdk/pkg/networkservice/common/filtermechanisms" "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/mechanismtranslation" + "github.com/networkservicemesh/sdk/pkg/networkservice/common/roundrobin" "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" registryclient "github.com/networkservicemesh/sdk/pkg/registry/chains/client" registryrecvfd "github.com/networkservicemesh/sdk/pkg/registry/common/recvfd" registrysendfd "github.com/networkservicemesh/sdk/pkg/registry/common/sendfd" + "github.com/networkservicemesh/sdk-kernel/pkg/kernel/networkservice/connectioncontextkernel" + "github.com/networkservicemesh/sdk-kernel/pkg/kernel/networkservice/ethernetcontext" + "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" @@ -73,15 +75,25 @@ 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, domain2Device map[string]string, clientDialOptions ...grpc.DialOption) endpoint.Endpoint { - nseClient := registryclient.NewNetworkServiceEndpointRegistryClient(ctx, clientURL, +func NewServer(ctx context.Context, tokenGenerator token.GeneratorFunc, vppConn Connection, tunnelIP net.IP, options ...Option) endpoint.Endpoint { + opts := &forwarderOptions{ + name: "forwarder-vpp-" + uuid.New().String(), + authorizeServer: authorize.NewServer(authorize.Any()), + clientURL: &url.URL{Scheme: "unix", Host: "connect.to.socket"}, + dialTimeout: time.Millisecond * 200, + domain2Device: make(map[string]string), + } + for _, opt := range options { + opt(opts) + } + nseClient := registryclient.NewNetworkServiceEndpointRegistryClient(ctx, opts.clientURL, registryclient.WithNSEAdditionalFunctionality( registryrecvfd.NewNetworkServiceEndpointRegistryClient(), registrysendfd.NewNetworkServiceEndpointRegistryClient(), ), - registryclient.WithDialOptions(clientDialOptions...), + registryclient.WithDialOptions(opts.dialOpts...), ) - nsClient := registryclient.NewNetworkServiceRegistryClient(ctx, clientURL, registryclient.WithDialOptions(clientDialOptions...)) + nsClient := registryclient.NewNetworkServiceRegistryClient(ctx, opts.clientURL, registryclient.WithDialOptions(opts.dialOpts...)) rv := &xconnectNSServer{} additionalFunctionality := []networkservice.NetworkServiceServer{ @@ -89,7 +101,7 @@ func NewServer(ctx context.Context, name string, authzServer networkservice.Netw sendfd.NewServer(), discover.NewServer(nsClient, nseClient), roundrobin.NewServer(), - stats.NewServer(ctx), + stats.NewServer(ctx, opts.statsOpts...), up.NewServer(ctx, vppConn), xconnect.NewServer(vppConn), connectioncontextkernel.NewServer(), @@ -101,20 +113,21 @@ func NewServer(ctx context.Context, name string, authzServer networkservice.Netw memif.WithDirectMemif(), memif.WithChangeNetNS()), kernel.MECHANISM: kernel.NewServer(vppConn), - vxlan.MECHANISM: vxlan.NewServer(vppConn, tunnelIP, vxlan.WithVniPort(tunnelPort)), + vxlan.MECHANISM: vxlan.NewServer(vppConn, tunnelIP, opts.vxlanOpts...), wireguard.MECHANISM: wireguard.NewServer(vppConn, tunnelIP), }), pinhole.NewServer(vppConn), connect.NewServer( client.NewClient(ctx, client.WithoutRefresh(), - client.WithName(name), - client.WithDialOptions(clientDialOptions...), - client.WithDialTimeout(dialTimeout), + client.WithName(opts.name), + client.WithDialOptions(opts.dialOpts...), + client.WithDialTimeout(opts.dialTimeout), client.WithAdditionalFunctionality( + cleanup.NewClient(ctx, opts.cleanupOpts...), mechanismtranslation.NewClient(), connectioncontextkernel.NewClient(), - stats.NewClient(ctx), + stats.NewClient(ctx, opts.statsOpts...), up.NewClient(ctx, vppConn), mtu.NewClient(vppConn), tag.NewClient(ctx, vppConn), @@ -123,9 +136,9 @@ func NewServer(ctx context.Context, name string, authzServer networkservice.Netw memif.WithChangeNetNS(), ), kernel.NewClient(vppConn), - vxlan.NewClient(vppConn, tunnelIP, vxlan.WithVniPort(tunnelPort)), + vxlan.NewClient(vppConn, tunnelIP, opts.vxlanOpts...), wireguard.NewClient(vppConn, tunnelIP), - vlan.NewClient(vppConn, domain2Device), + vlan.NewClient(vppConn, opts.domain2Device), filtermechanisms.NewClient(), pinhole.NewClient(vppConn), recvfd.NewClient(), @@ -135,8 +148,8 @@ func NewServer(ctx context.Context, name string, authzServer networkservice.Netw } rv.Endpoint = endpoint.NewServer(ctx, tokenGenerator, - endpoint.WithName(name), - endpoint.WithAuthorizeServer(authzServer), + endpoint.WithName(opts.name), + endpoint.WithAuthorizeServer(opts.authorizeServer), endpoint.WithAdditionalFunctionality(additionalFunctionality...)) return rv diff --git a/pkg/networkservice/mechanisms/vxlan/option.go b/pkg/networkservice/mechanisms/vxlan/option.go index 78a958a5..4b406ea5 100644 --- a/pkg/networkservice/mechanisms/vxlan/option.go +++ b/pkg/networkservice/mechanisms/vxlan/option.go @@ -1,6 +1,6 @@ -// Copyright (c) 2020-2021 Cisco and/or its affiliates. +// Copyright (c) 2020-2022 Cisco and/or its affiliates. // -// Copyright (c) 2021 Nordix Foundation. +// Copyright (c) 2021-2022 Nordix Foundation. // // SPDX-License-Identifier: Apache-2.0 // @@ -21,8 +21,8 @@ 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 { +// WithPort sets vxlan udp port +func WithPort(port uint16) Option { return func(o *vxlanOptions) { if port != 0 { o.vxlanPort = port