diff --git a/pkg/networkservice/common/mechanisms/kernel/client.go b/pkg/networkservice/common/mechanisms/kernel/client.go index 366aa3598..7d081ec3b 100644 --- a/pkg/networkservice/common/mechanisms/kernel/client.go +++ b/pkg/networkservice/common/mechanisms/kernel/client.go @@ -47,14 +47,18 @@ func NewClient(options ...Option) networkservice.NetworkServiceClient { func (k *kernelMechanismClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) { if !k.updateMechanismPreferences(request) { - request.MechanismPreferences = append(request.GetMechanismPreferences(), &networkservice.Mechanism{ + mechanism := &networkservice.Mechanism{ Cls: cls.LOCAL, Type: kernel.MECHANISM, Parameters: map[string]string{ kernel.NetNSURL: (&url.URL{Scheme: "file", Path: netNSFilename}).String(), - kernel.InterfaceNameKey: k.interfaceName, + kernel.InterfaceNameKey: GetNameFromConnection(request.GetConnection()), }, - }) + } + if k.interfaceName != "" { + mechanism.Parameters[kernel.InterfaceNameKey] = k.interfaceName + } + request.MechanismPreferences = append(request.GetMechanismPreferences(), mechanism) } return next.Client(ctx).Request(ctx, request, opts...) } diff --git a/pkg/networkservice/common/mechanisms/kernel/server.go b/pkg/networkservice/common/mechanisms/kernel/server.go index bd68b4f96..3c7c1bef4 100644 --- a/pkg/networkservice/common/mechanisms/kernel/server.go +++ b/pkg/networkservice/common/mechanisms/kernel/server.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020 Doc.ai and/or its affiliates. +// Copyright (c) 2020-2021 Doc.ai and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -39,6 +39,7 @@ func NewServer() networkservice.NetworkServiceServer { func (m *kernelMechanismServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) { if mechanism := kernel.ToMechanism(request.GetConnection().GetMechanism()); mechanism != nil { mechanism.SetNetNSURL((&url.URL{Scheme: "file", Path: netNSFilename}).String()) + mechanism.SetInterfaceName(GetNameFromConnection(request.GetConnection())) } return next.Server(ctx).Request(ctx, request) } diff --git a/pkg/networkservice/common/mechanisms/kernel/utils.go b/pkg/networkservice/common/mechanisms/kernel/utils.go new file mode 100644 index 000000000..614b17221 --- /dev/null +++ b/pkg/networkservice/common/mechanisms/kernel/utils.go @@ -0,0 +1,38 @@ +// Copyright (c) 2021 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 kernel + +import ( + "fmt" + + "github.com/networkservicemesh/api/pkg/api/networkservice" + kernelmech "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel" +) + +// GetNameFromConnection - returns a name computed from networkservice.Connection 'conn' +func GetNameFromConnection(conn *networkservice.Connection) string { + ns := conn.GetNetworkService() + nsMaxLength := kernelmech.LinuxIfMaxLength - 5 + if len(ns) > nsMaxLength { + ns = ns[:nsMaxLength] + } + name := fmt.Sprintf("%s-%s", ns, conn.GetId()) + if len(name) > kernelmech.LinuxIfMaxLength { + name = name[:kernelmech.LinuxIfMaxLength] + } + return name +} diff --git a/pkg/networkservice/common/mechanismtranslation/client_test.go b/pkg/networkservice/common/mechanismtranslation/client_test.go index f5ace3f4a..76c999f1e 100644 --- a/pkg/networkservice/common/mechanismtranslation/client_test.go +++ b/pkg/networkservice/common/mechanismtranslation/client_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020 Doc.ai and/or its affiliates. +// Copyright (c) 2020-2021 Doc.ai and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -37,7 +37,11 @@ import ( ) func kernelMechanism() *networkservice.Mechanism { - request := new(networkservice.NetworkServiceRequest) + request := &networkservice.NetworkServiceRequest{ + Connection: &networkservice.Connection{ + Id: "id", + }, + } _, _ = kernel.NewClient().Request(context.TODO(), request) return request.MechanismPreferences[0] }