From 9df6b14dc6092e6d81d4c19dbfbad61e4bb3d728 Mon Sep 17 00:00:00 2001 From: Ed Warnicke Date: Mon, 23 Nov 2020 19:24:46 -0600 Subject: [PATCH] Minor simplification around naming of interfaces and aliases. Signed-off-by: Ed Warnicke --- .../mechanisms/kernel/kerneltap/common.go | 18 ++--------- .../kernel/kernelvethpair/common.go | 16 ++-------- pkg/tools/mechutils/mechutils.go | 30 +++++++++++++++++++ 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/pkg/networkservice/mechanisms/kernel/kerneltap/common.go b/pkg/networkservice/mechanisms/kernel/kerneltap/common.go index 31f1df38..de5e432e 100644 --- a/pkg/networkservice/mechanisms/kernel/kerneltap/common.go +++ b/pkg/networkservice/mechanisms/kernel/kerneltap/common.go @@ -20,7 +20,6 @@ package kerneltap import ( "context" - "fmt" "time" "git.fd.io/govpp.git/api" @@ -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), @@ -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 @@ -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() diff --git a/pkg/networkservice/mechanisms/kernel/kernelvethpair/common.go b/pkg/networkservice/mechanisms/kernel/kernelvethpair/common.go index 2a7f8285..7bf19192 100644 --- a/pkg/networkservice/mechanisms/kernel/kernelvethpair/common.go +++ b/pkg/networkservice/mechanisms/kernel/kernelvethpair/common.go @@ -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() @@ -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 { diff --git a/pkg/tools/mechutils/mechutils.go b/pkg/tools/mechutils/mechutils.go index 80e3d606..282ae448 100644 --- a/pkg/tools/mechutils/mechutils.go +++ b/pkg/tools/mechutils/mechutils.go @@ -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" @@ -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 +}