Skip to content

Commit

Permalink
Update environment variables for vlan mechanism
Browse files Browse the repository at this point in the history
Signed-off-by: Laszlo Kiraly <laszlo.kiraly@est.tech>
  • Loading branch information
ljkiraly committed Jun 22, 2021
1 parent 450d0d2 commit 97667f8
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 13 deletions.
47 changes: 40 additions & 7 deletions pkg/kernel/networkservice/common/mechanisms/vlan/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,62 @@ package vlan
import (
"context"
"net/url"
"strconv"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/vlan"

"github.com/networkservicemesh/api/pkg/api/networkservice"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/log"
)

type vlanMechanismServer struct{}
type vlanMechanismServer struct {
baseInterface string
vlanTag int32
isOneLeg bool
}

// NewServer - creates a NetworkServiceServer that requests a vlan interface and populates the netns inode
func NewServer() networkservice.NetworkServiceServer {
return &vlanMechanismServer{}
func NewServer(baseInterface string, vlanID int32, oneLeg bool) networkservice.NetworkServiceServer {
v := &vlanMechanismServer{
baseInterface: baseInterface,
vlanTag: vlanID,
isOneLeg: oneLeg,
}
return v
}

func (m *vlanMechanismServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
if mechanism := vlan.ToMechanism(request.GetConnection().GetMechanism()); mechanism != nil {
mechanism.SetNetNSURL((&url.URL{Scheme: "file", Path: netNSFilename}).String())
func (v *vlanMechanismServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
log.FromContext(ctx).WithField("vlanMechanismServer", "Request").
WithField("VlanID", v.vlanTag).
WithField("BaseInterfaceName", v.baseInterface).
Debugf("request=", request)

if conn := request.GetConnection(); conn != nil {
if mechanism := vlan.ToMechanism(conn.GetMechanism()); mechanism != nil {
mechanism.SetNetNSURL((&url.URL{Scheme: "file", Path: netNSFilename}).String())
if conn.GetContext() == nil {
conn.Context = new(networkservice.ConnectionContext)
}
if conn.GetContext().GetEthernetContext() == nil {
conn.GetContext().EthernetContext = new(networkservice.EthernetContext)
}
ethernetContext := conn.GetContext().GetEthernetContext()
ethernetContext.VlanTag = v.vlanTag

if conn.GetContext().GetExtraContext() == nil {
request.Connection.Context.ExtraContext = map[string]string{}
}
extracontext := conn.GetContext().GetExtraContext()
extracontext["baseInterface"] = v.baseInterface
extracontext["isOneLeg"] = strconv.FormatBool(v.isOneLeg)
}
}
return next.Server(ctx).Request(ctx, request)
}

func (m *vlanMechanismServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
func (v *vlanMechanismServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
return next.Server(ctx).Close(ctx, conn)
}
46 changes: 42 additions & 4 deletions pkg/kernel/networkservice/mechanisms/vlan/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/rand"
"encoding/hex"
"io"
"strconv"

"github.com/networkservicemesh/api/pkg/api/networkservice"
vlanmech "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/vlan"
Expand All @@ -18,9 +19,20 @@ import (
func create(ctx context.Context, conn *networkservice.Connection, isClient bool) error {
if mechanism := vlanmech.ToMechanism(conn.GetMechanism()); mechanism != nil {
nsFilename := mechanism.GetNetNSURL()
if nsFilename == "" {
return nil
}
hostIfName := mechanism.GetInterfaceName(conn)
vlanID := mechanism.VlanID()
baseInterface := mechanism.GetBaseInterfaceName(conn)
vlanID, err := getVlanID(conn)
if err != nil {
return nil
}
baseInterface, ok := getBaseInterface(conn)
if !ok {
return nil
}
isOneLeg := getOneLegFlag(conn)

logger := log.FromContext(ctx).WithField("vlan", "create").
WithField("HostIfName", hostIfName).
WithField("HostNamespace", nsFilename).
Expand All @@ -29,10 +41,9 @@ func create(ctx context.Context, conn *networkservice.Connection, isClient bool)
WithField("isClient", isClient)
logger.Debug("request")

if nsFilename == "" || vlanID == 0 || baseInterface == "" {
if isOneLeg && isClient {
return nil
}

// TODO generate this based on conn id
tmpName, _ := generateRandomName(7)

Expand Down Expand Up @@ -71,6 +82,33 @@ func create(ctx context.Context, conn *networkservice.Connection, isClient bool)
return nil
}

func getVlanID(conn *networkservice.Connection) (int, error) {
if ethernetContext := conn.GetContext().GetEthernetContext(); ethernetContext != nil {
if ethernetContext.VlanTag != 0 {
return int(ethernetContext.VlanTag), nil
}
}
return 0, errors.New("no vlanID provided")
}
func getBaseInterface(conn *networkservice.Connection) (string, bool) {
if extraContext := conn.GetContext().GetExtraContext(); extraContext != nil {
if baseInterface, ok := extraContext["baseInterface"]; ok {
return baseInterface, true
}
}
return "", false
}
func getOneLegFlag(conn *networkservice.Connection) bool {
if extraContext := conn.GetContext().GetExtraContext(); extraContext != nil {
if strValue, ok := extraContext["isOneLeg"]; ok {
if flag, err := strconv.ParseBool(strValue); err == nil {
return flag
}
}
}
return false
}

func generateRandomName(size int) (string, error) {
id := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, id); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions pkg/kernel/networkservice/netnsconnectioncontext/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func AddIPs(ctx context.Context, conn *networkservice.Connection, isClient bool)
return errors.Wrapf(err, "invalid IP address: %v", ipContext.GetSrcIpAddr())
}

logger.Debugf("Is to set IP: %v and route", ipAddr)
logger.Debugf("Is to set IP: %v and routes: %+v", ipAddr, routes)
return setIPandRoutes(hostIfName, routes, ipAddr, currNetNS, clientNetNS)

}
Expand Down Expand Up @@ -96,7 +96,6 @@ func setIPandRoutes(ifName string, routes []*networkservice.Route, ipAddr *netli
IP: routeNet.IP,
Mask: routeNet.Mask,
},
Src: ipAddr.IP,
}); err != nil && !os.IsExist(err) {
return errors.Wrapf(err, "failed to add route: %v", route.GetPrefix())
}
Expand Down

0 comments on commit 97667f8

Please sign in to comment.