Skip to content

Commit

Permalink
Minor simplification around naming of interfaces and aliases.
Browse files Browse the repository at this point in the history
Signed-off-by: Ed Warnicke <hagbard@gmail.com>
  • Loading branch information
edwarnicke committed Nov 24, 2020
1 parent f9ea2d5 commit 9df6b14
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
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
30 changes: 30 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,31 @@ func ToNetlinkHandle(mechanism *kernel.Mechanism) (*netlink.Handle, error) {
}
return handle, nil
}

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)
}

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
}

0 comments on commit 9df6b14

Please sign in to comment.