Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor simplification around naming of interfaces and aliases. #21

Merged
merged 1 commit into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions pkg/networkservice/mechanisms/kernel/kerneltap/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package kerneltap

import (
"context"
"fmt"
"time"

"git.fd.io/govpp.git/api"
Expand Down Expand Up @@ -49,16 +48,6 @@ func create(ctx context.Context, conn *networkservice.Connection, vppConn api.Co
return err
}

// Naming is tricky. We want to name based on either the next or prev connection id depending on whether we
// are on the client or server side. Since this chain element is designed for use in a Forwarder,
// if we are on the client side, we want to name based on the connection id from the NSE that is Next
// if we are not the client, we want to name for the connection of of the client addressing us, which is Prev
namingConn := conn.Clone()
namingConn.Id = namingConn.GetPrevPathSegment().GetId()
if isClient {
namingConn.Id = namingConn.GetNextPathSegment().GetId()
}

now := time.Now()
tapCreateV2 := &tapv2.TapCreateV2{
ID: ^uint32(0),
Expand All @@ -67,7 +56,7 @@ func create(ctx context.Context, conn *networkservice.Connection, vppConn api.Co
TxRingSz: 1024,
RxRingSz: 1024,
HostIfNameSet: true,
HostIfName: mechanism.GetInterfaceName(namingConn),
HostIfName: mechutils.ToInterfaceName(conn, isClient),
HostNamespaceSet: true,
HostNamespace: nsFilename,
//TapFlags: 0, // TODO - TUN support for v3 payloads
Expand Down Expand Up @@ -112,10 +101,7 @@ func create(ctx context.Context, conn *networkservice.Connection, vppConn api.Co
WithField("duration", time.Since(now)).
WithField("netlink", "LinkByName").Debug("completed")

alias := fmt.Sprintf("server-%s", namingConn.GetId())
if isClient {
alias = fmt.Sprintf("client-%s", namingConn.GetId())
}
alias := mechutils.ToAlias(conn, isClient)

// Set the Link Alias
now = time.Now()
Expand Down
16 changes: 2 additions & 14 deletions pkg/networkservice/mechanisms/kernel/kernelvethpair/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,8 @@ func create(ctx context.Context, conn *networkservice.Connection, isClient bool)
// TODO - short circuit if already done
la := netlink.NewLinkAttrs()

// Naming is tricky. We want to name based on either the next or prev connection id depending on whether we
// are on the client or server side. Since this chain element is designed for use in a Forwarder,
// if we are on the client side, we want to name based on the connection id from the NSE that is Next
// if we are not the client, we want to name for the connection of of the client addressing us, which is Prev
namingConn := conn.Clone()
namingConn.Id = namingConn.GetPrevPathSegment().GetId()
if isClient {
namingConn.Id = namingConn.GetNextPathSegment().GetId()
}
la.Name = randstr.Hex(7)
alias := fmt.Sprintf("server-%s", namingConn.GetId())
if isClient {
alias = fmt.Sprintf("client-%s", namingConn.GetId())
}
alias := mechutils.ToAlias(conn, isClient)

// Create the veth pair
now := time.Now()
Expand Down Expand Up @@ -108,7 +96,7 @@ func create(ctx context.Context, conn *networkservice.Connection, isClient bool)
WithField("link.Name", name).
WithField("netlink", "LinkByName").Debug("completed")

name = mechanism.GetInterfaceName(namingConn)
name = mechutils.ToInterfaceName(conn, isClient)
// Set the LinkName
now = time.Now()
if err = handle.LinkSetName(l, name); err != nil {
Expand Down
34 changes: 34 additions & 0 deletions pkg/tools/mechutils/mechutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
package mechutils

import (
"fmt"
"net/url"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel"
"github.com/pkg/errors"
"github.com/vishvananda/netlink"
Expand Down Expand Up @@ -70,3 +72,35 @@ func ToNetlinkHandle(mechanism *kernel.Mechanism) (*netlink.Handle, error) {
}
return handle, nil
}

// ToInterfaceName - create interface name from conn for client or server side for forwarder.
// Note: Don't use this in a non-forwarder context
func ToInterfaceName(conn *networkservice.Connection, isClient bool) string {
// Naming is tricky. We want to name based on either the next or prev connection id depending on whether we
// are on the client or server side. Since this chain element is designed for use in a Forwarder,
// if we are on the client side, we want to name based on the connection id from the NSE that is Next
// if we are not the client, we want to name for the connection of of the client addressing us, which is Prev
namingConn := conn.Clone()
namingConn.Id = namingConn.GetPrevPathSegment().GetId()
if isClient {
namingConn.Id = namingConn.GetNextPathSegment().GetId()
}
return kernel.ToMechanism(conn.GetMechanism()).GetInterfaceName(namingConn)
}

// ToAlias - create interface alias/tag from conn for client or server side for forwarder.
// Note: Don't use this in a non-forwarder context
func ToAlias(conn *networkservice.Connection, isClient bool) string {
// Naming is tricky. We want to name based on either the next or prev connection id depending on whether we
// are on the client or server side. Since this chain element is designed for use in a Forwarder,
// if we are on the client side, we want to name based on the connection id from the NSE that is Next
// if we are not the client, we want to name for the connection of of the client addressing us, which is Prev
namingConn := conn.Clone()
namingConn.Id = namingConn.GetPrevPathSegment().GetId()
alias := fmt.Sprintf("server-%s", namingConn.GetId())
if isClient {
namingConn.Id = namingConn.GetNextPathSegment().GetId()
alias = fmt.Sprintf("client-%s", namingConn.GetId())
}
return alias
}